17
Quick Introduction to class in C++
A class is a data type that you define and you can also add some functions that work on this data type. That's it, that is exactly what a class is! Thank you for reading this article and I'll see you in the next one.
wait . . .
Above explanation is simple and covers what a class is in as little words as possible but there are more caveats related to it and I'll go over them in this article.
class Cat {
private:
// data memebers
string breed;
int legs;
public:
// constructor
Cat () {
breed = "Sphynx";
legs = 4;
meow = true;
}
// member function
void setLegs(int num) {
legs = num;
}
// member function
void setBreed(string breed_name) {
breed = breed_name;
}
};
What you create of classes are objects
. These objects
have certain properties that can be accessed and these can be defined in the class as data members. For a Table it can be wood type, number of drawers, length, height etc. By creating an object of a table, you can assign values to these properties in accordance with their data types.
A class is a plan, object it's execution
You might want your objects to perform some tasks and for that you write member functions. These functions are like regular functions accept they are only accessible to the object of the class; which means the functions can or can not take parameters, it can or cannot or return something back. Member functions are a powerful and you must use them with caution.
Inheritance in C++ can be achieved by deriving a class from another class of which access specifiers are an important part. I'll pick these topics up in my future articles. Stay tuned! If you want to read more about classes, head over here
Thanks for giving this article a read and I'll see you in the next one 😄
17