Instantiating class objects??

Reply

Join Date: Feb 2005
Posts: 4
Reputation: Lorita is an unknown quantity at this point 
Solved Threads: 0
Lorita Lorita is offline Offline
Newbie Poster

Instantiating class objects??

 
0
  #1
Feb 12th, 2005
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;
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: Instantiating class objects??

 
0
  #2
Feb 14th, 2005
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
  1. #include <iostream>
  2. using namespace std;

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 

using namespace std;


class Carpet
{
  public:
    int lengthInFeet;
    int widthInFeet;
    int area()
    {
      
      return (lengthInFeet * widthInFeet);//return the length times the width
      
    }
      
};


int main()//A thing of beauty
{
  
  Carpet mainCarpet;//declaring an instance of "Carpet"
  mainCarpet.lengthInFeet = 10;//setting the length
  mainCarpet.widthInFeet = 6;//setting the width
  cout<<mainCarpet.area();//printing out the return value of area
  cin.get();
  return 0;
  
}
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++

Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Instantiating class objects??

 
0
  #3
Mar 31st, 2005
thats a poor class, you should really have a constructor in there, and pass values using set and get functions.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Instantiating class objects??

 
0
  #4
Mar 31st, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Instantiating class objects??

 
0
  #5
Mar 31st, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Instantiating class objects??

 
0
  #6
Mar 31st, 2005
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Instantiating class objects??

 
0
  #7
Mar 31st, 2005
lol, I've often thought a good design is something that is ment to exsist and cant be made better....
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Instantiating class objects??

 
0
  #8
Mar 31st, 2005
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Instantiating class objects??

 
0
  #9
Mar 31st, 2005
Yeh true! Anyway we best stop there otherwise we are going a tad off topic!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC