derived class question

hi everybody, I have the following question: when I declare a class then define its member functions and member variables and then create another class(derived from the first one) I get a compiler error message like (`std::string Vehicle::Name' is private )The code is given below :

#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;

class Vehicle {
private:
string Name;
string model;
int year;
double price;
string engine_no;

public:
Vehicle (string N, string mod, int y, double pris, string engine );
Vehicle (string N, string mod, int y, double pris);
Vehicle (string N, string mod, int y);
Vehicle (string N, string mod);
Vehicle (string N);
Vehicle();
void Set_NMYPE (string N, string mod, int y, double pris, string engine);
void Set_NMYP (string N, string mod, int y, double pris);
void Set_NMY (string N, string mod, int y);
void Set_NM (string N, string mod);
void Set_N (string N);
friend void ShowObj (Vehicle &the_object);
friend void PrintObj(ostream &out_file, Vehicle &the_object);
};

class Bike : public Vehicle{
public:
Bike(string BN, string BM, int BY, double BP, string BE);
Bike(string BN, string BM, int BY, double BP);
Bike(string BN, string BM, int BY);
Bike(string BN, string BM);
Bike(string BN);
Bike();
};




int main(){

Vehicle MyVehicle("Mercedes");
Bike MikesBike("Suzuki","Nano");

ShowObj (MikesBike);


ofstream output;
output.open("vehicle.doc", ios::app);
if (output.fail()){
cout << "Error opening the file" << endl;
exit(1);
}
else
PrintObj(output, MyVehicle);

system("pause");
return 0;
}


Vehicle::Vehicle (string N, string mod, int y, double pris, string engine ){
Name = N;
model = mod;
year = y;
price = pris;
engine_no = engine;
}
Vehicle::Vehicle (string N, string mod, int y, double pris){
Name = N;
model = mod;
year = y;
price = pris;
engine_no = "N/A";
}
Vehicle::Vehicle (string N, string mod, int y){
Name = N;
model = mod;
year = y;
price = 0.0;
engine_no = "N/A";
}
Vehicle::Vehicle (string N, string mod){
Name = N;
model = mod;
year = 0;
price = 0.0;
engine_no = "N/A";
}
Vehicle::Vehicle (string N){
Name = N;
model = "N/A";
year = 0;
price = 0.0;
engine_no = "N/A";
}
Vehicle::Vehicle (){
Name = "N/A";
model = "N/A";
year = 0;
price = 0.0;
engine_no = "N/A";
}
void ShowObj (Vehicle &the_object){
cout << "===================================================" << endl;
cout << setw(20) << "Name: " << the_object.Name << endl;
cout << setw(20) << "Model: " << the_object.model << endl;
cout << setw(20) << "Year: " << the_object.year << endl;
cout << setw(20) << "Price: " << the_object.price << endl;
cout << setw(20) << "Engine_no: " << the_object.engine_no << endl;
cout << "===================================================" << endl;
}


void PrintObj(ostream &out_file, Vehicle &the_object){
out_file << "===================================================" << endl;
out_file << setw(20) << "Name: " << the_object.Name << endl;
out_file << setw(20) << "Model: " << the_object.model << endl;
out_file << setw(20) << "Year: " << the_object.year << endl;
out_file << setw(20) << "Price: " << the_object.price << endl;
out_file << setw(20) << "Engine_no: " << the_object.engine_no << endl;
out_file << "===================================================" << endl;
}
Bike :: Bike(string BN, string BM, int BY, double BP, string BE){
Name = BN;
model = BM;
year = BY;
price = BP;
engine_no = BE;

}
Bike :: Bike(string BN, string BM, int BY, double BP){
Name = BN;
model = BM;
year = BY;
price = BP;
engine_no = "N/A";
}
Bike :: Bike(string BN, string BM, int BY){
Name = BN;
model = BM;
year = BY;
price = 0.0;
engine_no = "N/A";
}
Bike :: Bike(string BN, string BM){
Name = BN;
model = BM;
year = 0;
price = 0.0;
engine_no = "N/A";
}
Bike :: Bike(string BN){
Name = BN;
model = "N/A";
year = 0;
price = 0.0;
engine_no = "N/A";
}
Bike :: Bike(){
Name = "N/A";
model = "N/A";
year = 0;
price = 0.0;
engine_no = "N/A";
}

// Is there something wrong with my definition of the derived class Bike ? Why doesnt it access the parent classes' member variables ?
My kindest regards !
You can only access public and protected members of the base class.
Change...

1
2
3
4
class Vehicle { 
private: 
string Name; 
string model; 


To...
1
2
3
4
class Vehicle { 
protected: 
string Name; 
string model; 


EDIT: You'll also need to #include <string> .
Last edited on
oh, that worked ! thanx a lot ! :))
Topic archived. No new replies allowed.