data Memeber(s) //apply Data Hiding //is Access Control

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Learning constructor in very simple way !

#include <iostream>
using namespace std;
class Car{
//Data Memeber(s)
//Apply data hiding
private: //is access control
int tire;
int power;
public:
Car();//Default
Car(int,int);//overloading /copy/parameterized
//Function Memeber(s)
//setter
void setData(int,int);
void setTire(int);
//getter
int getTire();
int getPower();
void display();//show all
};
void Car::setData(int t,int p)
{
tire=t;
power=p;
}
void Car::setTire(int t)
{
tire=t;
}
int Car::getTire()
{
return tire;
}
int Car::getPower()
{
return power;
}
void Car::display()
{
cout<<"Number of the tire :"<<tire<<endl;
cout<<"Power is :"<<power<<endl;
}
Car::Car()
{
tire=0;
power=0;
}
Car::Car(int t,int p)
{setData(t,p);
/*
tire=t;
power=p;

*/
}
void main()
{
Car mycar;
mycar.setData(4,200);
mycar.display();
cout<<"------ calling getter-------"<<endl;
Car yourcar;
yourcar.setData(4,500);
cout<<"Number of the tire :"<<yourcar.getTire()<<endl;
cout<<"Power is :"<<yourcar.getPower()<<endl;
//outside the class
cout<<"----Defualt one----"<<endl;
Car hiscar;
hiscar.display();
cout<<"----Copy one----"<<endl;
Car hercar(8,800);
hercar.display();
system("pause");
}

You might also like