CPU myCPU;
Screen myScreen;
Modem myModem;
to set these, the constructor of Laptop will need to have them passed in
class Laptop{private: CPU myCPU; Screen myScreen; Modem myModem;public: Laptop(CPU cpu, Screen screen, Modem modem);/* Other members here */};
NOTE the parameters to your Laptop constructor, there are three.
cpu which is a type of CPU
screen which is a type of Screen
modem whcih is a type of Modem
(Not the case differences)
now, In your main function
//Create a CPUCPU cpu(0.0,0.0,0.0); //I'm not worrying about the actual numbers//Create a ScreenScreen screen(0.0,0.0,0.0);//Create a modemModem modem(0.0,0.0,0.0);//Now, create your laptop instance and tell it about the 3 created instances aboveLaptop myLaptop(cpu, screen, modem);
Now you have an instance of Laptop that has been constructed properly.
Inside your Laptop constructor you can now access the modem, screen and cpu costs, weights and speed etc..
Laptop::Laptop(CPU cpu, Screen screen, Modem modem){ //etc.. myCPU = cpu; myScreen = screen; myModem = modem; //etc. obvisouly you'll actually want to use the results //of these i.e. double total_cost = myCPU.GetCost() + myScreen.GetCost() + myModem.GetCost();}
Hope this helps.
Keep up with this example as it also has more to learn from it that your not using.
Two things to look out for later are
1/ Already mentioned, passing my const reference in the constructors of Laptop.
2/ Using inheritence or polymorphism for Modem ,Screen, CPU. Maybe have a class called Component instead and Modem, screen, cpu can be different instances of Component or can be subclasses of component
Keep posting and asking until you get it, don't give up
[edited by - gommo on May 29, 2004 10:15:13 PM]