-: Class & Object in C++ :-
Class:-
In the C++, the classes are a building blocks along with their own properties and methods. Here Properties means Variables available inside and methods means Function available inside the class.
A class is a basic structure or can say a blueprint for a collection of data or information related to any specific kind of Subject.
For Example:- we have a car showroom with different cars & their properties like price, brand name, fuel type . So here A class will be created to store All Cars information with data related to the each car.
class CarInfo{
public:
DataType Price ;
DataType Brand;
DataType FuelType ;
};
Objects :-
An object is an instance of Class. Object is a real world entity that has any state and behavior. If it has any state means Definitely it would have data related to its state and behavior means Definitely it would have any functionality.
By using dott(.) operator after objectName we can access the property and methods of Class.
basics structure of Class with Object -
________________________________________________________________________
#include<iostream>
using namespace std;
class CarInfo{
public:
int Price ;
string BrandName ;
string FuelType;
FunctionName() {
Statements
}
}
int main() {
CarInfo car1, car2;
car1.Price = 1500000;
car2.Price = 8000000;
car1.FuelType = "Diesel" ;
car2.FuelType = "Petrol" ;
cout<< "price of car1 = " << car1.Price <<endl;
cout<< "car1 Fuel Type = " <<car1.FuelType <<endl;
cout<< "price of car2 = " << car2.Price <<endl;
cout<< "car2 Fuel Type = " <<car2.FuelType <<endl;
________________________________________________________________________
output:-
price of car1 = 1500000
car1 Fuel Type = Diesel
price of car2 = 8000000
car2 Fuel Type = Petrol
According to the definition , The Function inside the class is called as Method and Variable inside the Class is called Property or Attribute. Price, BrandName and FuelType are properties of created Class where FunctionName() is a function inside the same Class.
________________________________________________________________________
Click on image to see clear view
No comments:
Post a Comment