OOPs
OOPs
OOPS
Object-oriented programming OOP: Object-oriented programming (OOP) is a programming
paradigm based on the concept of "objects", which can contain data and methods: data in the
form of fields and method in the form of procedures. Programming starts with the concept of
real world objects and classes.
Feature of OOP
The 6 basic features are:
Class: A class is a blueprint for the object. We can think of a class as a sketch of a house. It
contains all the details about the floors, doors, windows, etc. Based on these descriptions we
build the house. House is the object.
Syntax:
Class className {
// some data
// some functions
};
For Example:
#include<iostream>
using namespace std;
class first {
public:
void fun() {
cout<< "Hello World!";
}
};
int main() {
first t;
cout<<"---------"<<endl;
t.fun();
return 0;
}
Objects: A class provides the blueprints for objects, so basically an object is created from a
class. We declare objects of a class with exactly the same sort of declaration that we declare
variables of basic types. To use the data and access functions defined in the class, we need to
create objects.
For Example:
#include<iostream>
using namespace std;
class first {
public:
void fun() {
cout<< "Hello World!";
}
};
int main() {
first t;
cout<<"\n---------1 "<<endl;
t.fun();
cout<<"\n---------2 "<<endl;
first g;
g.fun();
cout<<"\n---------3 "<<endl;
first k;
k.fun();
return 0;
}
For Example:
#include <iostream>
using namespace std;
class first {
public:
void fun() {
cout<< "Hello World!";
}
};
class MyClass {
public:
void myMethod();
};
void MyClass::myMethod() {
cout << "Hello World!"<<endl;
}
int main() {
first t;
MyClass myObj;
myObj.myMethod();
cout<<"---------"<<endl;
t.fun();
return 0;
}
Parameters: You can also add parameters:
For Example:
#include <iostream>
using namespace std;
class Car {
public:
int speed(int m);
};
Car::speed(int m) {
cout<<"This is function"<<endl;
return m;
}
int main() {
Car my; // Create an object of Car
cout << my.speed(200);
cout<<endl;
cout<< my.speed(432); // Call the method with an argument
return 0;
}
Inheritance: Inheritance can be defined as the process where one class acquires the
properties of another. With the use of inheritance the information is made manageable in a
hierarchical order.
The class which inherits the properties of other is known as subclass (derived class, child
class) and the class whose properties are inherited is known as superclass (base class, parent
class)
For Example:
class first
{
public:
int d;
void hello()
{
Cout<<”This is first file”;
}
};
class second : public first
{
public:
int c;
void bye()
{
Cout<<”This is second file”;
}
};
int main()
{
second obj;
obj.hello();
obj.bye();
return 0;
}
Types of Inheritance
Single inheritance: In single inheritance, a class is allowed to inherit from only one class.
For Example:
#include<iostream>
using namespace std;
class Vehicle {
public:
int hello()
{
cout << "This is a Vehicle\n";
}
};
class Car : public Vehicle {
};
int main()
{
Car obj;
Obj.hello();
return 0;
}
Multilevel inheritance: In this type of inheritance, a derived class is created from another
derived class.
For Example:
#include <iostream>
using namespace std;
class Vehicle {
public:
void fun() {
cout << "This is a Vehicle\n";
}
};
class fourWheeler : public Vehicle {
public:
void sun()
{
cout << "Objects with 4 wheels are vehicles\n";
}
};
class Car : public fourWheeler {
public:
void run()
{
cout << "Car has 4 Wheels\n";
}
};
int main()
{
Car obj;
obj.fun()
obj.sun();
obj.run();
return 0;
}
Multiple inheritances: Multiple Inheritance is a feature of C++ where a class can inherit
from more than one class. i.e one subclass is inherited from more than one base class.
For Example:
#include <iostream>
using namespace std;
class first
{
public:
void fun()
{
cout << "This is a first class\n";
}
};
class second {
public:
void run()
{
cout << "This is second class\n";
}
};
class third : public first, public second {
public:
void sun()
{
cout<<"This is third class \n";
}
};
int main()
{
third t;
t.fun();
t.run();
t.sun();
return 0;
}
Hierarchical inheritance: In this type of inheritance, more than one subclass is inherited
from a single base class. i.e. more than one derived class is created from a single base class.
For Example:
#include <iostream>
using namespace std;
class G {
public:
void fun()
{
cout << "This is a first class\n";
}
};
class B :public G {
public:
void run()
{
cout << "This is second class\n";
}
};
class E : public G {
public:
void sun()
{
cout<<"This is third class \n";
}
};
class A : public B{
public:
void first()
{
cout<<"\n this is class A\n";
}
};
class C:public B
{
public:
void second()
{
cout<<"\n this is class C \n";
}
};
class D : public E{
public:
void sit()
{
cout<<"\n this is class D \n";
}
};
class F: public E{
public:
void fit()
{
cout<<"\n this is class F \n";
}
};
int main()
{
A t; //this is 1 class
t.fun();
t.run();
C c; //this is 2 class
c.second();
c.fun();
D d; //this is 3 class
d.fun();
d.sun();
d.sit();
F f; //this is 4 class
f.fit();
f.fun();
f.sun();
return 0;
}
Hybrid inheritance: Hybrid Inheritance is implemented by combining more than one type of
inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance.
Below image shows the combination of hierarchical and multiple inheritances.
For Example:
#include <iostream>
using namespace std;
// base class
class Vehicle
{
public:
void hello()
{
cout << "This is a Vehicle\n";
}
};
// base class
class Fare
{
public:
void second()
{
cout << "Fare of Vehicle\n";
}
};
// first sub class
class Car : public Vehicle {
public:
void study()
{
Cout<<”This is car class\n”;
}
};
};
// main function
int main()
{
Bus o;
o.hello();
o.first();
o.study();
o.ccit();
return 0;
}
Polymorphism: The term "Polymorphism" is the combination of "poly" + "morphs" which
means many forms. It is a greek word. In object-oriented programming,Polymorphism is the
process of using same method name by multiple classes and redefines methods for the
derived classes. Polymorphism is the ability of an object to take on many forms. The most
common use of polymorphism in OOP occurs when a parent class reference is used to refer to
a child class object.
int ADD()
{
string a= "HELLO";
string b="SAM";
string c= a+b;
cout<<c<<endl;
}
};
class second
{
public:
int ADD(int X,int Y)
{
return X+Y;
}
int ADD()
{
string a= "HELLO";
string b="SAM";
string c= a+b;
cout<<c<<endl;
}
};
int main() {
first obj;
cout<<obj.ADD(128, 15)<<endl;
obj.ADD();
second obj2;
cout<<obj2.ADD(128, 15)<<endl;
obj2.ADD();
}
{
return X+Y;
}
int ADD()
{
string a= "HELLO";
string b="SAM";
string c= a+b;
cout<<c<<endl;
}
int ADD(String name)
{
cout<<”Name is “<<name;
}
};
int main() {
Addition obj;
cout<<obj.ADD(128, 15)<<endl;
obj.ADD();
obj.ADD(“CCIT”);
return 0;
}
Encapsulation: In general, encapsulation is a process of wrapping similar code in one place.
In C++, we can bundle data members and functions that operate together inside a single class.
For Example:
#include<iostream>
using namespace std;
class Encapsulation {
private:
int x;
public:
void setter(int a)
{
x = a;
}
int getter()
{
return x;
}
};
int main()
{
Encapsulation obj;
obj.set(5);
cout << obj.print()<<endl;
obj.set(10);
cout<<obj.print();
return 0;
}
Abstraction: It refers to, providing only essential information to the outside world and
hiding their background details. For example, a web server hides how it processes data it
receives, the end user just hits the endpoints and gets the data back.
For example:
#include <iostream>
using namespace std;
class Adder {
public:
Adder(int i = 0) {
total = i;
}
void addNum(int number) {
total += number;
}
int getTotal() {
return total;
};
private:
int total;
};
int main() {
Adder a;
a.addNum(10);
a.addNum(20);
a.addNum(30);
cout << "Total " << a.getTotal() <<endl;
}