Object Oriented Programming(OOPL) in C++ 2021 |
What is Object-Oriented Programming in C++?
Class: A C++ object Oriented Programming Language tries to map code instructions with real words making the code short and easier to understand.
class is a collection of data members and member functions.
Data members are the variable used inside the class.class is a blueprint from which objects are created an object is the instance of a class.
class is a collection of data members and member functions.
Data members are the variable used inside the class. Member functions are the function used inside the class.
Data members are the variable used inside the class. Member functions are the function used inside the class.
It is also called a User-defined data type.
👇👇👇
Java Oops Concept
Object: A Object is having states and behaviors in which state means what does it has and behavior means the object is an instruction of the class.
Object: A Object is having states and behaviors in which state means what does it has and behavior means the object is an instruction of the class.
when a class has been defined as a template. Memory is allocated only after object instructions.
Object-oriented Programming Language is solving a problem by creating an object is one of the popular approaches in this called the object-oriented Programming Language.
What is the Dry Concept?
Dry: A Dry Concept is called the Do Not Repeat Yourself.Some Important point making class program in c++
1. Declare variable with the data type.For example to find the area of the rectangle three variables are(height, width, and area).
2. Declare function.
for example, to find the area of a rectangle single function finds the area() is sufficient.
3. Create an object of the class to access the data member and member function.
class name object name;
Example: Rectangle Data;
Here Rectangle is the name of the class and Data is the name of the object.
4. Accessing data member and member function
Data can be accessed dot(.) Operator.
5. Accessing data member
Data.height;
5. Accessing data member
Data.height;
6. Accessing member function
Data.findArea();
How To Use Class and Object in C++?
#include<iostream>
using namespace std;
class Rectangle
{
public:
int height;
int width;
int area;
void findArea()
{
area=height*width;
cout<<"Area of Rectangle:"<<area;
}
};
int main()
{
Rectangle data;
data.height=20;
data.width=20;
data.findArea();
}
*****OUTPUT*****
Area of Rectangle:400
How To Area Of Rectangle Use Class and Object in C++?
#include<iostream>
using namespace std;
class Rectangle
{
public:
int height;
int width;
int area;
void getPara()
{
height=20;
width=20;
}
void findArea()
{
area=height*width;
cout<<"Area of Rectangle:"<<area;
}
};
int main()
{
Rectangle data;
data.getPara();
data.findArea();
}
*****OUTPUT*****
Area of Rectangle:400
Constructor: It is a special member function of a class that executes when we create the instance(object) of that class. in other words, we can say that there is no need to call a constructor.
Its name is the same as the class name. It has no return type. It may be parameterized or non-parameterized. It is used to initialize the class level variable.
How To Use Constructor in C++ With Example?
#include<iostream>
using namespace std;
class Rectangle
{
public:
int height;
int width;
int area;
Rectangle()
{
height=20;
width=20;
area=height*width;
cout<<"Area of Rectangle:"<<area;
}
};
int main()
{
Rectangle rec1;
}
*****OUTPUT*****
Area of Rectangle:400
Types of constructor
There are three types of constructors.1. Default Constructor.
2. Parameterized Constructor.
3. Copy Constructor.
Default Constructor: The constructor with no parameter is called the default constructor.
How To Use Default Constructor in C++ With Example?
#include<iostream>
using namespace std;
Rectangle
{
public:
int height;
int width;
int area;
Rectangle()
{
height=20;
width=20;
area=height*width;
cout<<"Area of Rectangle:"<<area;
}
};
int main()
{
Rectangle rec1;
}
*****OUTPUT*****
Area of Rectangle:400
Parameterized Constructor: The constructor with the parameter is called the parameterized constructor.
How To Use Parameterized Constructor in C++ with Example?
#include<iostream>
using namespace std;
class Rectangle
{
public:
Rectangle(int height,int width)
{
area=height*width;
cout<<"Area of Rectangle:"<<area;
}
};
int main()
{
Rectangle rec1(20,20);
}
*****OUTPUT*****
Area of Rectangle:400
Copy Constructor: In this type of constructor one object with a parameter is copied into another object so it is called a copy constructor.
How To Use Copy Constructor in C++ With Example?
#include<iostream>
using namespace std;
class Rectangle
{
public:
int area;
Rectangle(int height,int width)
{
area=height*width;
}
void Display()
{
cout<<"Area of Rectangle:"<<area<<endl;
}
};
int main()
{
Rectangle r1(20,20);
r1.Display();
Rectangle r2=r1;
r2.Display();
}
*****OUTPUT*****
Area of Rectangle:400
Area of Rectangle:400
Destructor: It is a special member function of a class that executes when the work of an object goes out of scope.
In another word, we can say that there is no need to call a destructor. Its name is the same as the class name.
It has no return type and no parameter. It is used to destroy class-level variables. Tilde symbol(~) is used to declare destructor.
How To Use Destructor in C++ With Example?
#include<iostream>
using namespace std;
class Destructordata
{
public:
Destructordata()
{
cout<<"I am inside constructor"<<endl;
}
~Destructordata() //Destructor
{
cout<<"I am inside destructor";
}
};
int main()
{
Destructordata d;
}
*****OUTPUT*****
I am inside Constructor
I am inside Destructor