Hi, guys.
I'm somewhat confused with inheritance yet. Please check my code first, it works just fine.
PHP Code:
#include <iostream>
#include <string>
class Vehicle
{
public:
Vehicle(string, int, int);
Vehicle(){cout << "undefined vehicle" << endl;};
void profile();
private:
string name;
int wheel;
int speed;
};
Vehicle :: Vehicle(string s, int w, int sp)
{
name=s;
wheel=w;
speed=sp;
cout << "the name of the vehicle present is: " << name << endl;
cout << "it has " << w << " wheels." << endl;
cout << "its speed is " << sp << " km/h." << endl;
}
void Vehicle :: profile()
{
cout << "welcome here, I'll introduce the vehicle later." << endl;
}
class Car : public Vehicle
{
public:
Car(string, int, int, int);
Car(){ cout << "undefined car" << endl; };
private:
int price;
};
Car :: Car(string s, int w, int sp, int p) : Vehicle(s, w, sp)
{
price=p;
cout << "its price is " << price << " dollars." << endl;
}
void main()
{
Car myCar1;
cout << endl;
Car myCar2("bicycle", 2, 15, 0);
}
Now, 2 questions are waiting your suggestions:
1 when I create a object with the Car class, the old constructor with base class Vehicle are called first. we could see it from the output. Could I get ride of those which belong to base class and display only the contents of derived class ?
2 the first constructor of derived class has 4 arguments, and 3 of them belong to the base class Vehicle, so I write something like this: : Vehicle(s, w, sp) but what about when we need of part of those arguments ? for example, we just want pass s and sp from the old class. I tried : Vehicle(s, sp) but got an error.