+ 1
Pls help. I understand NOTHING about classes. Explain to me. C++
What are they for? It will be better if you can explain in Russian. But someone, by any language HELP.
3 Respuestas
0
You can think of a class as an object (not visible but data containers) which can hold variables and can execute functions. When you declare a class it becomes it's own data type. Inside a class a function is called a method. Also methods and variables are called properties.
Consider if we needed to represent fruits. We could make a class called 'Fruit' then set a couple properties.
class Fruit
{
///// seedCount is a property
private : int seedCount;
///// this is the constructor
public : Fruit(int seeds){
seedCount = seeds;//set
};
///// printSeeds is a method
public : void printSeeds(){
cout << seedCount;//print
};
};
int main() {
Fruit fruit(5); // declares fruit var
fruit.printSeeds ();// calls method
return 0;
}
'public Fruit' is declaring the class and everything in the brackets would be it's properties. A property uses the syntax..
accessType : propertyType propertyName;
The method declared is public and returns nothing so is void.
0
You can then take advantage of inheritance and polymorphism to make specific classes of Fruit.. Apple, Orange, and so on.