// Example C++ code for OPT// From: http://www.cprogramming.com/tutorial/lesson12.html#include <iostream>using namespace std;class Computer // Standard way of defining the class{public:// This means that all of the functions below this(and any variables)// are accessible to the rest of the program.// NOTE: That is a colon, NOT a semicolon...Computer();// Constructor~Computer();// Destructorvoid setspeed ( int p );int readspeed();protected:// This means that all the variables under this, until a new type of// restriction is placed, will only be accessible to other functions in the// class. NOTE: That is a colon, NOT a semicolon...int processorspeed;};// Do Not forget the trailing semi-colonComputer::Computer(){//Constructors can accept arguments, but this one does notprocessorspeed = 0;}Computer::~Computer(){//Destructors do not accept arguments}void Computer::setspeed ( int p ){// To define a function outside put the name of the class// after the return type and then two colons, and then the name// of the function.processorspeed = p;}int Computer::readspeed(){// The two colons simply tell the compiler that the function is part// of the classreturn processorspeed;}int main(){Computer compute;// To create an 'instance' of the class, simply treat it like you would// a structure. (An instance is simply when you create an actual object// from the class, as opposed to having the definition of the class)compute.setspeed ( 100 );// To call functions in the class, you put the name of the instance,// a period, and then the function name.cout<< compute.readspeed();// See above note.}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。