Inheritence

#include<iostream.h>
#include<string.h>

class Vehicle{
	double weight;
	int speed;


public:
	Vehicle(double w, int s):weight(w),speed(s){}

	void accelerate(){
		setSpeed( getSpeed() + 10);
	}
	void setSpeed(int s){
		speed += s;
	}
	int getSpeed(){
		return speed;
	}

};

class Car: public Vehicle{
	char model[10];

public:

	Car(double w, int s, char m[10]):Vehicle(w,s){
		strcpy(model,m);
	}

	void accelerate(){
		setSpeed(getSpeed() + 50);
	}
};

void main(){
	Vehicle generic(1000,100);
	Car mycar(800, 400, "McLaren");

	cout<<"Generic: "<<generic.getSpeed()<<endl;
	cout<<"Car: " << mycar.getSpeed()<<endl;

	generic.accelerate();
	mycar.accelerate();

   	cout<<"Generic: "<<generic.getSpeed()<<endl;
	cout<<"Car: " << mycar.getSpeed()<<endl;

   	generic.accelerate();
	mycar.accelerate();

   	cout<<"Generic: "<<generic.getSpeed()<<endl;
	cout<<"Car: " << mycar.getSpeed()<<endl;

   	generic.accelerate();
	mycar.accelerate();

   	cout<<"Generic: "<<generic.getSpeed()<<endl;
	cout<<"Car: " << mycar.getSpeed()<<endl;
}
Advertisements

About praveenaj

I'm just an average guy who enjoys doing Programming/Web Designs/Pencil art & drinking coffee :)
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s