![]() |
| ||
| Instantiating class objects?? Hi. I can't figure out what I'm doing wrong here. I know I have to instantiate a Carpet object, assign values then pass the instance to the printArea func. Easier said than done! Thanks for any help!! L #include<iostream.h> #include<conio.h> class Carpet { public: int lengthInFeet; int widthInFeet; }; void main() { int lengthInFeet=12; int widthInFeet=12; int total; total=lengthInFeet*widthInFeet; getch(); } void printArea(total) { cout<<"The area of carpet is "total"sq. feet."<<endl; } |
| ||
| Re: Instantiating class objects?? Ah where to begin: 1. void main is wrong it is int main! 2. If you are going to use standard headers do it this way #include <iostream> 3.Use code tags like I did above they make everyone who reads your post happy. Read This. As for your question to declare an instance of your class you just do this #include <iostream>//Ah the way it should be |
| ||
| Re: Instantiating class objects?? thats a poor class, you should really have a constructor in there, and pass values using set and get functions. |
| ||
| Re: Instantiating class objects?? >and pass values using set and get functions If possible, set and get member functions should be avoided. The only advantage is that data members can be accessed through a controlled interface, but a well designed class will hide its internals properly, thus making the need for get and set member functions unnecessary. Here are a few guidelines for designing classes, concerning data members: 1) All data members are private. 2) If possible, access to data members should not rely on the type of data. 3) All outside forces (including derived types) should work through a public (or protected) interface only. |
| ||
| Re: Instantiating class objects?? All my teaching in class's and OOP have used the idea of set and get fuctions are needed... I could post up alot of my work if you wish? |
| ||
| Re: Instantiating class objects?? That's generally how OOP is taught, and get/set methods have their place, but teachers (and as a result, their students) often take it to the extreme to the detriment of good design. |
| ||
| Re: Instantiating class objects?? lol, I've often thought a good design is something that is ment to exsist and cant be made better.... |
| ||
| Re: Instantiating class objects?? There are degrees of goodness. Long ago, monolithic programs with GOTO statements all over creation was considered good design. Imagine how hard programming would be today if nobody tried to make it better through structured programming techniques, OOP, service oriented design, component oriented design, design patters, and the like. |
| ||
| Re: Instantiating class objects?? Yeh true! Anyway we best stop there otherwise we are going a tad off topic! |
| All times are GMT -4. The time now is 4:04 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC