Hi all,
1) In a problem I have to solve, it is required that I create a class with one of the functions in it that returns the total cost of all objects instantiated from that class. Here's the class definition:
Code:
class Computer {
public:
Computer(char * = "", double = 0.0, double = 0.0, int = 0, int = 0);
~Computer();
void setPersonName(char *);
void setCost(float);
void setCpuSpeed(float);
void setMonths(int);
void setMemorySize(int);
char *getPersonName();
float getCost();
float getCpuSpeed();
int getMonths();
int getMemorySize();
float sell();
void ageInSeconds();
void printTotalCost();
private:
char* personName;
double cost;
double cpuSpeed;
int months;
int memorySize;
static double totalCost;
};
The function I am referring to is pringTotalCost(). Anyways, I used the static double totalCost for this purpose and it worked fine; however, is it possible to call the function by just typing "printTotalCost()" instead of computer1.printTotalCost() for example?
2) How come this works:
[code]
Computer::Computer(char *name, double price, double speed, int age, int mem)
{
personName = name;
cost = price;
cpuSpeed = speed;
months = age;
memorySize = mem;
totalCost += price;
}[code]
But not this:
Code:
Computer::Computer(char *name, double price, double speed, int age, int mem)
{
setPersonName(name);
setCost(price);
setCpuSpeed(speed);
setMonths(age);
setMemorySize(mem);
totalCost += price;
}
3) How come this outputs the same number of characters I enter regardless of how small I set the size of the array?
Code:
char name[10];
cin >> name;
cout << name;
I am using MS VS 6.0
Thanks