class functions code example
Example 1: c++ class method example
class Object {
public:
int var;
void setVar(int n) {
var = n;
}
int getNum() {
return var;
}
};
int main() {
Object obj;
obj.setVar(13);
std::cout << obj.getNum() << std::endl;
return 0;
}
Example 2: Using functions in Class
class Student{
int scores[5];
public:
void input(){
for(int i=0; i<5; i++){
cin >> scores[i];
}
}
int calculateTotalScore(){
int total = 0;
for(int i=0; i<5; i++){
total += scores[i];
}
return total;
}
};
Example 3: calling a method on an object c++
MyClass m;
m.printInformation();
Example 4: c++ How many functions (methods) can a class have?
As many as you want