Class is a crucial part of a type of programming called the Object-Oriented Programming (OOP). Programming itself has two common types: procedural programming (writing procedures and functions) and OOP (using classes and objects). OOP has several advantages over procedural programming:
First, let's learn what are classes and objects in OOP. Classes are templates for objects, while objects are instances of the class. For example, we have a basket of fruit containing apple, mango, and orange. The class is the 'fruit', while the objects are 'apple', 'mango', and 'orange'. In the class, there are also attributes and methods. To put it simply, the attributes are variables belonging to a class, while methods are functions belonging to the class.
Syntax:
class class_name{ access_specifier: //class members (attributes or methods) };
Access specifier is the keyword that specifies the scope of the members of the class. There are three access specifiers in C++:
public: members are accessible from outside of the classprivate: members cannot be accessed from outside the classprotected: members cannot be accessed from outside the class,
but they can be accessed in the inherited classes.Example:
#include<iostream> class MyClass { // The class public: // Access specifier int myNum; // Attribute (int variable) string myString; // Attribute (string variable) }; int main() { return 0; }
Syntax: class_name object_name;
To access the attributes of the class, we have to use the dot syntax
(object_name.attribute).
Example:
#include<iostream> class MyClass { // The class public: // Access specifier int myNum; // Attribute (int variable) string myString; // Attribute (string variable) }; int main() { MyClass myObj; // Create an object of MyClass // Access attributes and set values myObj.myNum = 15; myObj.myString = "Some text"; return 0; }
We can declare methods inside of the class or outside of the class definition. And, to call the method, we use the same way as calling attributes (using the dot syntax).
Example of the inside of the class method:
#include<iostream> class MyClass { // The class public: // Access specifier int myNum; // Attribute (int variable) void greet(){ std::cout<<"Hello!"; } }; int main() { MyClass myObj; // Create an object of MyClass // Access attributes and set values myObj.myNum = 15; myObj.greet(); return 0; }
Example of the outside of the class method:
#include<iostream> class MyClass { // The class public: // Access specifier int myNum; // Attribute (int variable) }; void MyClass::greet(){ std::cout<<"Hello!"; } int main() { MyClass myObj; // Create an object of MyClass // Access attributes and set values myObj.myNum = 15; myObj.greet(); return 0; }
Constructors is a special method that is automatically called when an object of a class is created. Constructors have the same name as the class and it is always public. Constructors also do not have any return value.
Syntax:
class_name (){ //lines of code }
Example:
#include<iostream> class MyClass { // The class public: // Access specifier MyClass() { // Constructor std::cout << "Hello World!"; } }; int main() { MyClass myObj; // Create an object of MyClass (this will call the constructor) return 0; }
Encapsulation ensures sensitive data hidden from users. To do this,
we just need to make the class access specifiers private.
And, if you want others to read or modify the value of a
private member, you can provide public
get and set methods.
Example:
#include<iostream> class Cube { private: // Private attribute int side; public: // Setter void setSide(int s) { side = s; } // Getter int getSide() { return side; } }; int main() { Cube myObj; myObj.setSide(5); std::cout << myObj.getSide(); return 0; }
Inheritance means inheriting the attributes and methods from one class to another. We say that the class that inherits from another class is a derived class (child class), while the class being inherited from is the base class (parent class).
Example:
#include<iostream> class MySchool { // The base class public: int members; void welcome(){ std::cout<< "Welcome!"; } }; class MyClass: public MySchool { // The derived class public: char name = 'b'; }; int main() { MyClass myObj; myObj.welcome(); return 0; }
Polymorphism enables users to use methods to perform different tasks. This allows users to perform a single action in different ways.
Example:
#include<iostream> #include<string> // Base class class Animal { public: void animalSound() { std::cout << "The animal makes a sound \n"; } }; // Derived class class Pig : public Animal { public: void animalSound() { std::cout << "The pig says: wee wee \n"; } }; // Derived class class Dog : public Animal { public: void animalSound() { std::cout << "The dog says: bow wow \n"; } }; int main() { Animal myAnimal; Pig myPig; Dog myDog; myAnimal.animalSound(); myPig.animalSound(); myDog.animalSound(); return 0; }