473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Cout will not print variable

All of my variables are printing properly when the program runs....except string color....why is this?

//program create vehicle, prompt user to fill values, once user is done adding objects to "parking lot" prints associated values for each object//

#include <iostream>
#include <string>
#include <vector>
using namespace std;


//VEHICLE CLASS//
class Vehicle
{

public:
Vehicle();
void printFeatures();
void change_price(double thenewprice);
void change_year(int thenewyear);
void change_numberOfDoors( int thenewnumberOfDoors);
void change_color(string thenewcolor);
void print_color(string anothercolor);
string get_color();
private:

double price;
int year;
int numberOfDoors;
double mpg;
string color; //variable that refuses to print with cout//

};
//SUV CLASS//
class SUV: public Vehicle
{
public:
SUV();
void print_hybrid2();
private:
bool isHybrid;
};
//SEDAN CLASS//
class Sedan: public Vehicle
{

public:
Sedan();
void print_hybrid();
private:
bool isHybrid;
};

//SPORTS CAR CLASS//
class SportsCar: public Vehicle
{
public:
SportsCar();
private:

};
//VEHICLE CONSTRUCTOR DEFINITION//
Vehicle::Vehicle()
{
price=1000;
year=1986;
numberOfDoors=4;
mpg=20;
color="brown";
}
//VEHICLE FUNCTION DEFINTIONS//
void Vehicle::change_price( double thenewprice)
{
price= thenewprice;
}
void Vehicle::change_year(int thenewyear)
{
year= thenewyear;
}
void Vehicle::change_numberOfDoors(int thenewnumberOfDoors)
{
numberOfDoors = thenewnumberOfDoors;
}
void Vehicle:: change_color(string thenewcolor)
{
color = thenewcolor;
}
void Vehicle:: print_color(string anothercolor)
{
cout << anothercolor;
}
string Vehicle:: get_color()
{
return color;
}
//SEDAN AND SUV MEMBER FUNCTIONS//
void Sedan:: print_hybrid()
{
if (isHybrid == true)
{
cout << " Hybrid: Yes";
}
if (isHybrid == false)
{
cout << " Hybrid: No";
}

}
void SUV:: print_hybrid2()
{

if (isHybrid == true)
{
cout << " Hybrid: Yes";
}
if (isHybrid == false)
{
cout << " Hybrid: No";
}

}
//SPORTSCAR CONSTRUCTOR DEFINTION//
SportsCar::SportsCar()
{
int numberOfDoors1=2;
int year1;
int price1;
string color1;
cout << "What is the color of the car?" << "\n";
cin >> color1;
cout << "What is the price of the car?" << "\n";
cin >> price1;
cout << "What is the year of the car?" << "\n";
cin >> year1;
change_year(year1);
change_color(color1);
change_price(price1);
change_numberOfDoors(numberOfDoors1);
}
//SEDAN CONSTRUCTOR DEFINTION//
Sedan::Sedan()
{

string Hybridornot;
int year;
int price;
string color1;
cout << "What is the color of the car?" << "\n";
cin >> color1;
cout << "What is the price of the car?" << "\n";
cin >> price;
cout << "What is the year of the car?" << "\n";
cin >> year;
cout << "Is the car a hybrid?" << "\n";
cin >> Hybridornot;
if (Hybridornot=="Yes"||Hybridornot=="yes")
{
isHybrid=true;
}
if (Hybridornot=="No"||Hybridornot=="no")
{
isHybrid=false;
}
change_year(year);
change_price(price);
change_color(color1);

}
// SUV CONSTRUCTOR DEFINTION//
SUV::SUV()
{
int mgp=10;
string Hybridornot;
int year;
int price;
string color;
cout << "What is the color of the car?" << "\n";
cin >> color;
cout << "What is the price of the car?" << "\n";
cin >> price;
cout << "What is the year of the car?" << "\n";
cin >> year;
cout << "Is the car a hybrid?" << "\n";
cin >> Hybridornot;
if (Hybridornot=="Yes"||Hybridornot=="yes")
{
isHybrid=true;
}
if (Hybridornot=="No"||Hybridornot=="no")
{
isHybrid=false;
}
change_year(year);
change_price(price);
change_color(color);
}


//PRINT FEATURES FUNTION DEFITION//
void Vehicle::printFeatures()
{
cout << "Price: " << price << " Year: " << year << " Color: " << get_color() << "Number of Doors: " << numberOfDoors << " mgp: " << mpg;

}



int main ()
{
//ONE VECTOR FOR EACH TYPE OF VEHICLE (OR RATHER FOR EACH DERIVED CLASS)//
vector<SUV*> SUVs;
vector<Sedan*> Sedans;
vector<SportsCar*> SportsCars;
string answer1;
string answer2;
do{
cout << "Would you like to add a car to the lot? Enter Yes or No." << "\n";
cin >> answer1;
if (answer1=="No"||answer1=="no")
{
break;
}
cout <<"What typ of car of would you like to add to the the lot? Please enter Sedan, Suv, or SportsCar." << "\n";
cin >> answer2;
if (answer2=="Sedan" || answer2=="sedan")
{
Sedan newSedan;
Sedan* ThenewSedan;
ThenewSedan = &newSedan;
Sedans.push_back(ThenewSedan);
}
if (answer2=="Suv"||answer2=="suv"|| answer2=="SUV")
{
SUV newSUV;
SUV* ThenewSUV;
ThenewSUV = &newSUV;
SUVs.push_back(ThenewSUV);
}
if (answer2=="SportsCar"||answer2=="sportscar")
{
SportsCar newSportsCar;
SportsCar* ThenewSportsCar;
ThenewSportsCar = &newSportsCar;
SportsCars.push_back(ThenewSportsCar);
}
}
while(answer1=="yes"||answer1=="Yes");

//AFTER EXITING THE LOOP, PRINT THE CARS AND ASSOCIATED STATISTICS//

for(int i=0; i<SportsCars.size(); i++)
{
SportsCars[i]->printFeatures();
cout << "\n";
}
for(int i=0; i<Sedans.size(); i++)
{
Sedans[i]->printFeatures();
Sedans[i]->print_hybrid();
cout << "\n";
}
for(int i=0; i<SUVs.size(); i++)
{
SUVs[i]->printFeatures();
SUVs[i]->print_hybrid2();
cout << "\n";
}
;
return 0;
}
Jan 27 '12 #1
3 2473
weaknessforcats
9,208 Expert Mod 8TB
color is a string and is private to Vehicle. It will cout just fine if you use a Vehicle member function. Otherwise, you would call your

Vehicle::get_color() to get the color:

So in Sportscar your would:

Expand|Select|Wrap|Line Numbers
  1. string SportsCar::get_Color()
  2. {
  3.     return Vehicle::get_color();
  4. }
Since you are no using virtual functions, your derived class s hiding your base class. The color is art of Vehicle so there is no color in SportsCar. Sportscar uses Vehicle::color by calling the correct Vehicle methods to set or get the color.
Jan 29 '12 #2
Unfortunately that's part of the problem. The function i defined within the base class is not returning the string color for the object Sportscar( despite the fact that I used the member function defined for the base Vehicle class). I appreciate the time you took to respond. I took your code and tried it. The syntax works, but no dice still. The mind blowing part is i modeled it the same way i did for the variable price and year(which are working perfectly).(what i tried included)
//loop printing at end//
for(int i=0; i<SportsCars.size(); i++)
{
SportsCars[i]->printFeatures();
cout << SportsCars[i]->get_Color(); //get_color(doesn't work here above either)//
cout << "\n";
}
//function defined//
string SportsCar::get_Color()
{
return Vehicle::get_color();
}
Jan 30 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
This works:

Expand|Select|Wrap|Line Numbers
  1. class Vehicle
  2. {
  3.    string color;
  4.  
  5. public:
  6.        void setColor(string& val);
  7.        string getColor();
  8.  
  9. };
  10.  
  11. class SportsCar :public Vehicle
  12. {
  13. public:
  14.        void setColor(string& val);
  15.        string getColor();
  16.  
  17. };
  18.  
  19. void Vehicle::setColor(string& val)
  20. {
  21.     color = val;
  22. }
  23. string Vehicle::getColor()
  24. {
  25.     return color;
  26. }
  27.  
  28. void SportsCar::setColor(string& val)
  29. {
  30.     Vehicle::setColor(val);
  31. }
  32. string SportsCar::getColor()
  33. {
  34.     return Vehicle::getColor();
  35. }
  36.  
  37. ////////////////////////
  38. int main()
  39. {
  40.      SportsCar s;
  41.      s.setColor(string("blue"));
  42.      cout << "It's a  " << s.getColor() << " car." << endl;
  43. }
  44.  
  45.  
Jan 31 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Michael Krasnyk | last post by:
Hi, How can I print variable values by their names? For example: import sys for str in dir(sys): print str Will be printed only variable names, but I need values from list of variable...
20
by: CoolPint | last post by:
While I was reading about const_cast, I got curious and wanted to know if I could modify a constant variable through a pointer which has been "const_cast"ed. Since the pointer would be pointing to...
10
by: Gurikar | last post by:
How to make cout not printing on the console. i mean void main() { cout<<"Hello world"<<endl; } Is there any way where i can block printing on console even when iam
6
by: nish.parikh | last post by:
Hi, I am using std::cout to print a char pointer that is NULL. Subsequent calls to std::cout dont print anything. Is this the expected behavior? example: #include <iostream> int main( int...
5
by: Ashish Sharma | last post by:
#include <iostream.h> #include <stdio.h> void main() { cout<<"hello"; printf("hi"); }
2
by: Sam | last post by:
Hi, In the query analyser if I type the following: DECLARE @SortOrder integer exec pr_Admin_SetFieldSortOrder @SortOrder print @SortOrder @SortOrder is never printed. I just get the message...
3
by: Jess | last post by:
Is there a way to print a variable's data type - like int, string, dbl?
2
by: surendersharma | last post by:
pls send me answer of above question on my email id .this code should be only in c#.et window base not web base. thank u ...
16
by: lisadean | last post by:
Dear Group I have the database set up, and the query, which lists my name and addresses. Each time I attempt to create a report I fail to create it right. I want a simple layout, a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.