05 Object Oriented Concept
05 Object Oriented Concept
Another Example:-
What are Classes and Objects?
Another Example:-
Everything in C++ is associated with classes and objects, along with its
attributes and methods
To Create an object of MyClass, specify the class name, followed by the object name.
int main() {
MyClass myObj; // Create an object of MyClass
To Create an object of MyClass, specify the class name, followed by the object name.
int main() {
MyClass myObj1,myObj2; // Create an object of MyClass
myObj1.myNum = 15;
myObj1.myString = “XYZ";
class MyClass {
myObj2.myNum = 17;
public:
myObj2.myString = “ABC";
int myNum;
string myString;
cout << myObj1.myNum << "\n";
};
cout << myObj1.myString;
• Inside Example
class MyClass {
int main() {
public:
MyClass myObj;
void myMethod() {
myObj.myMethod();
cout << "Hello World!";
return 0;
}
}
};
Create Class Methods with Parameters
#include <iostream>
using namespace std;
class Car {
public:
int speed(int maxSpeed);
};
int main() {
Car myObj; // Create an object of Car
cout << myObj.speed(200); // Call the method with an argument
return 0;
}
C++ Access Specifiers
class MyClass {
int x; // Private attribute
int y; // Private attribute
};
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};
C++ Constructors
// Print values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}
C++ Copy Constructors
class Complex{
private:
int a,b;
public:
Complex(int x,int y){ int main()
a=x; {
b=y; Complex
cout<<" Constructor 2 Parameter "<<“\n a="<<a<<"b="<<b
C1(5,6),C2(10),C3(11,12);
}
Complex(int x){ Complex C4;
a=x; Complex C5=C1;
cout<<"Constructor 1 Parameter \n"<<"a="<<a<<endl; return 0;
} }
Complex(){
cout<<"Constructor 0 Parameter \n";
}
Complex(Complex &c){
a=c.a;
b=c.b;
}
};
C++ Destructor
• Similarly, like a friend function, a class can also be made a friend of another class using
keyword friend.
class A{ int main()
private: {
int a,b; A t1;
public: B t2;
void setData(int x,int y) t1.setData(9,10);
{ t2.showData(t1);
a=x; }
b=y;
} • When a class is made a friend class, all the member
friend class B; //Friend keyword
functions of that class becomes friend functions.
};
class B{ • In this program, all member functions of class B will be
public: friend functions of class A. Thus, any member function of
void showData(A t) class B can access the private and protected data of class
{ A. But, member functions of class A cannot access the data
cout<<"a="<<t.a<<"\n b="<<t.b; of class B.
} • Remember, friend relation in C++ is only granted, not
}; taken.
C++ Nested Classes in C++
• A nested class is a class that is declared in another class. The nested class is also a member variable of the enclosing
class and has the same access rights as the other members. However, the member functions of the enclosing class
have no special access to the members of a nested class.
int main() {
#include<iostream>
cout<<"Nested classes in C++"<< endl;
using namespace std;
A :: B obj;
class A {
obj.getdata(9);
public:
obj.putdata();
class B {
return 0;
private:
}
int num;
public: • In the above program, class B is defined inside the class A so it is
void getdata(int n) { a nested class. The class B contains a private variable num and
num = n;
two public functions getdata() and putdata(). The function
}
void putdata() {
getdata() takes the data and the function putdata() displays the
cout<<"The number is "<<num; data.
} • In the function main(), an object of the class A and class B is
}; defined. Then the functions getdata() and putdata() are called
}; using the variable obj.
Function Overloading
With function overloading, multiple functions can have the same name with different
parameters:
int main() {
int myNum1 = plusFuncInt(8, 5);
double myNum2 = plusFuncDouble(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
return 0;
}
Function Overloading
• Instead of defining two functions that should do the same thing, it is better to overload one.
• In the example below, we overload the plusFunc function to work or both int and double:
int main() {
int myNum1 = plusFunc(8, 5);
double myNum2 = plusFunc(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
return 0;
}
Encapsulation
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To
achieve this, you must declare class variables/attributes as private (cannot be accessed from
outside the class). If you want others to read or modify the value of a private member, you
can provide public get and set methods.
int main() {
class Employee { Employee myObj;
private: myObj.setSalary(50000);
// Private attribute cout << myObj.getSalary();
int salary; return 0;
}
public:
// Setter
void setSalary(int s) { Why Encapsulation?
salary = s; •It is considered good practice to declare your class
} attributes as private (as often as you can).
// Getter Encapsulation ensures better control of your data,
int getSalary() { because you (or others) can change one part of the
return salary; code without affecting other parts
} •Increased security of data
};
Inheritance
In C++, it is possible to inherit attributes and methods from one class to another. We group the
"inheritance concept" into two categories:
•derived class (child) - the class that inherits from another class
•base class (parent) - the class being inherited from
To inherit from a class, use the : symbol.
// Base class
class Vehicle { int main() {
public: Car myCar;
string brand = “XYZ"; myCar.move();
void move() { cout << myCar.brand + " " + myCar.model;
cout << “car is moving \n" ; return 0;
} }
};
// Derived class
class Car: public Vehicle {
public:
string model = “ABC";
};
Polymorphism
Polymorphism means "many forms", and it occurs when we have many classes that are
related to each other by inheritance.
Class a
{
public:
void display(){ cout << "hello"; }
}
Class b:public a
{
public:
void display(){ cout << "bye";};
}
Function Overriding
It is the redefinition of base class function in its derived class with same signature i.e return type
and parameters. It can only be done in derived class.
Class A
{
public:
void display(){ cout << "hello"; }
void show(){ cout << “Parent Show"; }
}
Class B:public a
{
public:
void display(){ cout << "bye";};
}
int main()
{
B obj;
obj.display();
obj.show();
}
Function Overloading Vs Function Overriding
The keyword struct introduces the structure definition. Next comes the structure name or tag, which is part. The
declarations of the structure members—model number, part number, and cost—are enclosed in braces. A semicolon
follows the closing brace, terminating the entire structure.