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 {
public: myObj2.myNum = 17;
int myNum; myObj2.myString = “ABC";
string myString;
}; cout << myObj1.myNum << "\n";
cout << myObj1.myString;
• Inside Example
class MyClass {
public: int main() {
void myMethod() { MyClass myObj;
cout << "Hello myObj.myMethod();
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 main() {
public: // Public access
MyClass myObj;
specifier
myObj.x = 25; // Allowed (public)
int x; // Public attribute
myObj.y = 50; // Not allowed
private: // Private access
(private)
specifier
return 0;
int y; // Private attribute
}
};
C++ Access Specifiers
class MyClass {
int x; // Private attribute
int y; // Private attribute
};
// 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; int main()
public: {
Complex(int x,int y){ Complex
a=x;
C1(5,6),C2(10),C3(11,12);
b=y;
} Complex C4;
Void show() Complex C5=C1;
{ return 0;
cout<<" Constructor 2 Parameter "<<“\n a="<<a<<"b="<<b }
}
Complex(Complex &c){
a=c.a;
b=c.b;
}
};
C++ Destructor
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 / Abstraction
Encapsulation in C++ is implemented as a class that bundles data and the functions operating on this
data together. Mostly data is declared as private so that it is not accessible outside the class. The methods or
functions are declared as public and can be accessed using the object of the class.
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) { Abstraction: The concept of abstraction only
salary = s; shows necessary information to the users. It
} reduces the complexity of the program by hiding
// Getter the implementation complexities of programs.
int getSalary() {
return salary;
}
};
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 Animal { // Base class
public: int main() {
void animalSound() { Animal myAnimal;
cout << "The animal makes a sound \ Pig myPig;
n" ; Dog myDog;
}
}; myAnimal.animalSound();
class Pig : public Animal { myPig.animalSound();
public: myDog.animalSound();
void animalSound() { return 0;
cout << "The pig says: wee wee \ }
n" ;
}
}; Why “Inheritance” and “Polymorphism”?
class Dog : public Animal {
- It is useful for code reusability: reuse attributes
public:
void animalSound() { and methods of an existing class when you create
cout << "The dog says: bow wow \ a new class.
n" ;
}
};
Function Overriding
Whenever we writing function in base and derived class in such a way that function name,
parameter must be same called function overriding.
Class a
{
public:
void display(){ cout << " hello"}
};
Class b:public a
{
public:
void display(){ cout << " hello"}
};
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 fun(){ cout << “Ankit" <<endl; }
};
Class B:public a
{
public:
void fun(){ cout << “Ankit" <<endl; }
};
int main()
{
B obj;
obj.fun();
Obj.A::fun();
return 0;
}
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.