Code:
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
//dinh nghia lop Car
class car
{
private :
int speed; //toc do
char *mark; //nhan hieu
float price; //gia ca
public:
void setmark(char *);
char *getmark();
void setspeed(int);
int getspeed();
void setprice(float);
float getprice();
car(int speedin=0,char *markin="" ,float pricein=0);
void show();
~car();
};
car::car(int speedin, char *markin, float pricein)
{
setspeed(speedin);
mark = new char [strlen(markin)+1]; //cap phat dong cho nhan hieu xe
strcpy(mark,markin);
setprice(pricein);
}
void car::setmark(char *markin)
{
mark = new char [strlen(markin)+1];
strcpy(mark,markin);
}
char *car::getmark()
{
return mark;
}
void car::setspeed(int speedin)
{
speed=speedin;
}
int car::getspeed()
{
return speed;
}
void car::setprice(float pricein)
{
price=pricein;
}
float car::getprice()
{
return price;
}
void car::show()
{
cout << "day la hang xe " << mark << "co toc do "
<< speed <<" km/h va voi gia " << price << "$\n";
}
car::~car()
{
delete mark;
}
//dinh nghia lop bus ke thua tu lop car, them kieu label
class bus:public car
{
private:
int label; //so hieu tuyen xe bus
public:
void setlabel(int);
int getlabel();
bus(int speedin=0, char *markin="", float pricein=0, int labelin=0);//khoi tao tham so day du
void show();
};
bus::bus(int speedin, char *markin, float pricein, int labelin):car(speedin, markin, pricein)
{
setlabel(labelin);
}
void bus::setlabel(int labelin)
{
label = labelin;
}
int bus::getlabel()
{
return label;
}
void bus::show()//dinh nghia chong phuong thuc show() cua lop co so
{
car::show();
cout << "tuyen so " << label<< endl;
}
/*
int main()
{
bus mybus;
int speedin, labelin;
float pricein;
char markin[20];
cout<<"nhap nhan hieu xe";
cin>>markin;
cout<<"nhap toc do xe";
cin>>speedin;
cout<<"nhap gia tri cua xe";
cin>>pricein;
cout<<"nhap hieu so xe";
cin>>labelin;
mybus.setmark(markin);
mybus.setspeed(speedin);
mybus.setprice(pricein);
mybus.setlabel(labelin);
mybus.show();
return 0;
}
*/
int main()
{
car c(120,"Honda civic",20000);
c.show();//phuong thuc cua lop car
bus b(100,"\Huyndai",15000,26);
b.getlabel();
b.show();//phuong thuc cua lop bus
cout<<"\nchuyen doi kieu giua lop dan xuat va lop co so"<<endl;
c=b;
c.show();
return 0;
}