+ 1
Abstraction, OOP, Classes
Can someone please explain the basics of these? I am in this lesson but I cannot figure out what they mean and how they work, especially setting up classes. Help!!!
3 Answers
+ 1
The 3 keywords in OOP (object oriented programming): Inheritance, Polymorphism and Encapsulation. If you don't know about these, ask me or look it up on the internet. A class is basically a plan for creating an object: What does your object need, what should it be able to do...? The class for the object "dog" for example should provide 4 legs; body; head; skin color, the method "bark"... (if your creating a fantasy game for example you don't have to keep things real/ dog could have 8 legs...).
+ 1
class MyClass {
public: // for public objects
void bark() {
std::cout << "woof!";
}
MyClass(int i) { // constructor, called when an instance of the class is created
std::cout << i;
}
~ MyClass() { // destructor, called when an instance of the class is destroyed
std::cout << "bye!";
}
private: // for private objects
int legs = 4;
int heads = 1;
}; // remember the semicolon
0
So what is the syntax for writing a class? Can you just do class .... bark 4 legs head?