Object Oriented Programming in
C++
● Procedural programming is about writing procedures or
functions
● Object-oriented programming is about creating objects that
contain both data and functions.
OOP advantages over procedural
programming:
● OOP is faster & easier to execute.
● OOP provide clear structure for prgram.
● OOP makes code easier to maintain,modify and debug.
● OOP helps to keep c++ code DRY(Don't Repeat Yourself).
Class: Class is a template for objects
Object: Object is an instance of a class.
Example:- class: CAR
object: Audi,Toyota,BMW
Create a class
class MyClass { // The class
public: // Access specifier
int number; // Attribute (int variable)
string str; // Attribute (string variable)
};
Create an Object
class MyClass { // The class
public: // Access specifier
int number; // Attribute (int variable)
string str; // Attribute (string variable)
};
int main() {
MyClass myObj; // Create an object of MyClass
// Access attributes and set values
myObj.number = 15;
myObj.str = "Contain Some Text";
// Print attribute values
cout << myObj.number << "\n";
cout << myObj.str;
return 0;
}
Class Methods:-
● Inside class definition
class MyClass {
public:
void myMethod() {
// Method/function defined inside the class
cout << "Hello World!";
}
};
● Outside class definition
class MyClass {
public:
void myMethod();
// Method/function declaration
};
// Method/function definition outside the class
void MyClass::myMethod() {
cout << "Hello World!";
}
Constructors
It is a special method that is automatically called when an object
of a class is created.
For creating a Constructor use the same name as the class
followed by paretheses ( ) :
class MyClass {
public:
// Constructor
MyClass() {
cout << "Hello Raghav!";
}
};
KEY Points: Contructor has same name as the class, don't have
any return value, Always Public:
Constructors can also take parameters (just like regular
functions), which can be useful for setting initial values for
attributes.
class Car {
public:
string brand;
string model;
int year;
Car(string x, string y, int z) {
// Constructor with parameters
brand = x;
model = y;
year = z;
}
};
Just like functions, constructors can also be defined outside the
class.
class Car {
public:
string brand;
string model;
int year;
// Constructor declaration
Car(string x, string y, int z);
};
// Constructor definition outside the class
Car::Car(string x, string y, int z) {
brand = x;
model = y;
year = z;
}
Access Specifiers
In C++, there are three access specifiers:
● Public - Members are accessible from outside the class
● Private - Members cannot be accessed (or viewed) from
outside the class
● Protected - members cannot be accessed from outside the
class, however, they can be accessed in inherited classes.
KEY Points: By default class are private if you don't specify an
access specifier
Encapsulation
● Encapsulation is defined as the wrapping up of data under a
single unit.
● It is the mechanism that binds together code and the data.
● Hidding the sensitive Data from user.
● Encapsulation ensures better control of your data, because
you (or others) can change one part of the code without
affecting other parts
● To achieve this Class/Variables/Attributes declare as private.
KEY Points: To access a private attribute, use public "get" and
"set" methods
class Employee {
private:
int salary;
public:
void setSalary(int s) {
salary = s;
}
int getSalary() {
return salary;
}
};
Inheritance
Inheritance is the process in which two classes have an
relationship among each other and objects of one class acquire
properties and features of the other class.
We group the "inheritance concept" into two categories:
● derived class(child/subclass) - the class that inherits from
another class
● base class(parent/superclass) - the class being inherited
from
Example: Class Vehicle is the parent class, and Class Bus, Car,
and Bike are child classes.
Multilevel Inheritance
MyGrandChild is derived from class MyChild (which is derived
from MyClass)
// Base class (parent)
class MyClass {
public:
void myFunction() {
cout << "Some content in parent class." ;
}
};
// Derived class (child)
class MyChild: public MyClass {
};
// Derived class (grandchild)
class MyGrandChild: public MyChild {
};
int main() {
MyGrandChild myObj;
myObj.myFunction();
return 0;
}
Multiple Inheritance
Class derived from more than one base class,using a
Comma-separated list
// Base class
class MyClass {
public:
void myFunction() {
cout << "Some content in parent class." ;
}
};
// Another base class
class MyOtherClass {
public:
void myOtherFunction() {
cout << "Some content in another class." ;
}
};
// Derived class
class MyChildClass: public MyClass, public
MyOtherClass {
};
int main() {
MyChildClass myObj;
myObj.myFunction();
myObj.myOtherFunction();
return 0;
}
KEY Points: Protected can be accessed in the inherited class
Single Inheritance
derived class inherits the properties of a single base class.
class Animal {
public:
void eat() {
std::cout << "Animal is eating" <<
std::endl;
}
};
class Dog : public Animal {
public:
void bark() {
std::cout << "Dog is barking" <<
std::endl;
}
};
int main() {
Dog myDog;
myDog.eat(); // output: Animal is eating
myDog.bark(); // output: Dog is barking
return 0;
}
Hierarchical inheritance
In hierarchical inheritance, multiple derived classes inherit from a
single base class.
class Shape {
public:
void draw() {
std::cout << "Drawing a shape" <<
std::endl;
}
};
class Circle : public Shape {
public:
void drawCircle() {
std::cout << "Drawing a circle" <<
std::endl;
}
};
class Square : public Shape {
public:
void drawSquare() {
std::cout << "Drawing a square" <<
std::endl;
}
};
int main() {
Circle myCircle;
Square mySquare;
myCircle.draw(); // output: Drawing a shape
mySquare.draw(); // output: Drawing a shape
myCircle.drawCircle(); // output: Drawing a
circle
mySquare.drawSquare(); // output: Drawing a
square
return 0;
}
Inheritance can help reduce code duplication and improve code
reusability.
Polymorphism
Polymorphism means "many forms", and it occurs when we have
many classes that are related to each other by inheritance.
Polymorphism is achieved through the use of virtual functions.
Two types:-
Compile Time Polymorphism:-
Also known as overloading ,early binding and static binding
class A { // base
class declaration.
int a;
public:
void display()
{ cout<< "Class A "; }
};
class B : public A { //
derived class declaration.
int b;
public:
void display(){
cout<<"Class B"; }
};
Run Time Polymorphism
Also known as overriding, dynamic binding and late binding.
#include <iostream>
using namespace std;
class Animal {
public:
void eat()
{cout<<"Eating..."; }
};
class Dog: public Animal{
public:
void eat()
{ cout<<"Eating bread";}
};
int main(void) {
Dog d = Dog();
d.eat();
return 0;
}
Output:
Eating bread