Object Oriented Programming Practical 15

Question 1
Write a program which show the use of constructor in derived class.
car.cpp
C
				#include<iostream.h>
#include<conio.h>
#include<string.h>

class car
{
	char car_type[100];

	public:
	car(char c_type[30])
	{
		strcpy(car_type,c_type);
	}

	void display()
	{
		cout<<"\nCar c_type: "<<car_type;
	}
};

class brand:public car
{
	float speed;
	char brand_name[50];

	public:
	brand(float sp,char name[30],char c_type[30]):car(c_type)
	{
		speed=sp;
		strcpy(brand_name,name);
	}

   void putdata()
	{
		cout<<"\nCar brand name: "<<brand_name;
		cout<<"\nCar speed : "<<speed;
   }
};

int main()
{
	clrscr();
	char c_type[30],c_brand[30];
	float sp;
	cout<<"Enter car type: ";
	cin>>c_type;
	cout<<"Enter car brand: ";
	cin>>c_brand;
	cout<<"Enter car speed: ";
	cin>>sp;
	brand br(sp,c_brand,c_type);
	br.display();
	br.putdata();
	getch();
	return 0;
}

			
Output

Leave a Comment

Your email address will not be published. Required fields are marked *