C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 31st, 2003, 12:25 AM
Sonic98 Sonic98 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Memphis, TN
Posts: 199 Sonic98 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 7 sec
Reputation Power: 11
Send a message via AIM to Sonic98 Send a message via Yahoo to Sonic98
MySpace
Array of Class

How do you implement an array of classes. I have created a class. I then defined an array of that type. But I can't figure out how to access the data members I want to.

Let's say my class is called "people."

I defined an array

people webdesigners[25];


If I have a member function called "getnames" I know that I can do this

webdesigners[i].getnames

That way I can collect the information for 25 web designers


My problem is that I need to sort the webdesigners by name


How can I do this?

I can't say

strcmp(webdesigners[1].last,webdesigners[2].getnames)

Because only member fuctions have access to the data members

Can you define a class within itself

for example could say

SortNames(people[]);

Reply With Quote
  #2  
Old March 31st, 2003, 01:17 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
Hi,

It's not clear whether your getnames() function prompts the user for input, or whether it is a typical "get" function that retrieves a private data member for an object. Assuming it's the latter, you can sort by name very easily. Every name is accessible through the get function, so you could do something like this:

strcmp(webdesigners[1].getnames(),webdesigners[2].getnames())

Then you would have to create a temporary object and use a copy constructor and an assignment operator to perform switching in the array.

Reply With Quote
  #3  
Old March 31st, 2003, 08:45 AM
Sonic98 Sonic98 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Memphis, TN
Posts: 199 Sonic98 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 7 sec
Reputation Power: 11
Send a message via AIM to Sonic98 Send a message via Yahoo to Sonic98
MySpace
The getnames prompts the user for the names. What I am really asking is how you pass an array of classes between functions.

I know that usually when you define a class you can define your member fuctions

Public:
getnames();
sortnames();
Private
char first[15];
char last[15];

Because the member functions already have access to the data members.

You'd only have to define the functions like this

getnames(char[],char[]);
sortnames(char[],char[]):

if you were gonna use a different variable name within the function to avoid putting bad data in your data members

ex.
People:Getnames(char fst[], char lst[])


So, you're telling me that if I define an array of the class, I don't have to make the array a parameter of a function? How would I pass the array from function to function

If I do this
People:Getnames()
{
People webdesigners[25];
for(i=0;i<=25,i++)
{
cout<<"\nenter the name:";
cin>>people[i].first>>people[i].last;
}

This would allow me to input a first and last name into 25 instances of the class

But how would I pass that array of classed to the sortnames function ?

Can I define the functions

Public:
getnames(people[]);
sortnames(people[]);
Private
char first[15];
char last[15];

Reply With Quote
  #4  
Old March 31st, 2003, 08:54 AM
Sonic98 Sonic98 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Memphis, TN
Posts: 199 Sonic98 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 7 sec
Reputation Power: 11
Send a message via AIM to Sonic98 Send a message via Yahoo to Sonic98
MySpace
What if I defined a 2nd class with the first class as a datamember?

Reply With Quote
  #5  
Old March 31st, 2003, 12:13 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
i would make an array of pointers to People objects a member of the people class, like this:

private:

People *webmasters[25];


then in the constuctor for People, do this:

for(int i = 0; i < 25; i++)
{
webmasters[i] = new People();
}

then fill up the objects with ur getfunction like this:

People:Getnames()
{

for(i=0;i<25,i++)
{
cout<<"\nenter the name:";
cin>>webmasters[i]->first>>webmasters[i]->last;
}
}
-then create ur sorting function for that class, and there is no need for it to have any array parameters b/c all the info is stored in ur class! i m pretty sure all that will work, im kinda in a rush so i cant try any of the code. but if it doesnt there's plenty of people here much smarter than me who will know. good luck

Reply With Quote
  #6  
Old March 31st, 2003, 01:02 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
infamous41md,

then in the constuctor for People, do this:

for(int i = 0; i < 25; i++)
{
webmasters[i] = new People();
}


You wouldn't do that in a constructor: you would have an infinite series of constructions because the constructor calls itself.

Sonic98,

What I am really asking is how you pass an array of classes between functions.

Just like any other array.

Can I define the functions

Public:
getnames(people[]);
sortnames(people[]);
Private
char first[15];
char last[15];


Yes. You can define your functions any way you want. In the function definition of course, you're going to need to provide a parameter name for the array, so you can reference it in the function body.

Last edited by 7stud : March 31st, 2003 at 01:22 PM.

Reply With Quote
  #7  
Old March 31st, 2003, 03:34 PM
Sonic98 Sonic98 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Memphis, TN
Posts: 199 Sonic98 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 7 sec
Reputation Power: 11
Send a message via AIM to Sonic98 Send a message via Yahoo to Sonic98
MySpace
Quote:
Originally posted by infamous41md
i and there is no need for it to have any array parameters b/c all the info is stored in ur class! i m pretty sure all that will work, im kinda in a rush so i cant try any of the code. but if it doesnt there's plenty of people here much smarter than me who will know. good luck


But if I made an array of the class or pointer to the class wouldn't each ivalue be seen as a different class? So you're say an array of a class is treated just as a single instance of the class? I'll try what you said on my compiler

Reply With Quote
  #8  
Old March 31st, 2003, 04:24 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
One solution would be to have a swap function within a class to allow for swapping of data of the current class with another class - a basic requirement of any sorting algorithm.

Another solution, is if you don't care to sort the actual classes, but simply wish to print the names to the screen in sorted order is to create an array of pointers to classes, which can be sorted according to any accessable data of the classes (assuming that a comparing function for that data is available).

Reply With Quote
  #9  
Old March 31st, 2003, 04:30 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
"But if I made an array of the class or pointer to the class wouldn't each ivalue be seen as a different class? So you're say an array of a class is treated just as a single instance of the class?"

You're having a lot of trouble with terminology, so it's hard to figure out exactly what you're trying to do. You don't have an "array of a classes", rather you have an array of objects of a given class. And, yes, each i value will either refer to (or point to) a different object. When you declare an array of objects, the default constructor creates an object corresponding to each i value. If you put a statement like:

cout<<"Constructor called.\n";

inside your default constructor and then declare an array of objects like this:

People web_designers[25];

then you will see that the default constructor was called 25 times.

Here are some questions for you to consider:

1)Do you want a different object for each web designer, or do you want the array of web designers to be a member of one object?

2)Will the user always enter the same number of names, or do you need to dynamically create objects or members to handle a variable number of names?

Last edited by 7stud : March 31st, 2003 at 04:40 PM.

Reply With Quote
  #10  
Old March 31st, 2003, 05:10 PM
Sonic98 Sonic98 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Memphis, TN
Posts: 199 Sonic98 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 7 sec
Reputation Power: 11
Send a message via AIM to Sonic98 Send a message via Yahoo to Sonic98
MySpace
The purpose of this is to sort the designers by last name. That is why I am trying to do it as an array of People ojbects. When I sort it I want to be able to compare two last names and if the 2nd one should be before the first, swap the entire object. So, I want to swap web_designers[0] and web_designers[1]. I don't want to have to swap the data members individually.

I don't want to have to swap
web-designers.first
web-designers.last
web-designers.ssn
web-designers.level

for each person.

You see the problem is I am converting someone else's program. I am converting thier structs over to classes. It's simpler and quicker to do with struct because the data members are public by default.

Reply With Quote
  #11  
Old March 31st, 2003, 05:10 PM
Sonic98 Sonic98 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Memphis, TN
Posts: 199 Sonic98 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 7 sec
Reputation Power: 11
Send a message via AIM to Sonic98 Send a message via Yahoo to Sonic98
MySpace
Quote:
Originally posted by Jason Doucette
One solution would be to have a swap function within a class to allow for swapping of data of the current class with another class - a basic requirement of any sorting algorithm.



That is actually what I was thinking also.

Reply With Quote
  #12  
Old March 31st, 2003, 09:03 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
yea u could do what Jason said. create another class that was a utility class basically. have this class member(s) be an array of pointers to People objects. then swapping would be as ez as creating a temp pointer and flopping pointers. u could make it even cooler by overloading the > and < operators so u could compare people objects directly, like:

temp *peoplePtr
if(people[i] > people[i + 1])
peoplePtr = people[i];
people[i] = people[i + 1];
people[i + 1] = peoplePtr;

something like that for example.

Reply With Quote
  #13  
Old April 1st, 2003, 02:09 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
"yea u could do what Jason said. create another class that was a utility class basically."

Or, you could define a get function for the People class that gives you access to the last name data member, and then sort the array.

"It's simpler and quicker to do with struct because the data members are public by default"

Which might lead one to ask: why don't you just make the data members in the class public?

Last edited by 7stud : April 1st, 2003 at 02:11 AM.

Reply With Quote
  #14  
Old April 1st, 2003, 09:47 AM
Sonic98 Sonic98 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Memphis, TN
Posts: 199 Sonic98 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 7 sec
Reputation Power: 11
Send a message via AIM to Sonic98 Send a message via Yahoo to Sonic98
MySpace
1. If I compare people[1] to people[2] that would be comparing the whole class. I just want it to compare last names


2. I'm not making everything public in the class because they don't want it that way.

But the ways some of you suggest will likely work.

Reply With Quote
  #15  
Old April 1st, 2003, 12:36 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
"1. If I compare people[1] to people[2] that would be comparing the whole class. I just want it to compare last names."

You can compare last names by defining a function called getLastName() and then comparing people[1].getLastName() to people[2].getLastName(). Or, if you want to compare objects, you can define the comparison operators to only check last names. Those class operator functions would also give you access to the private members.

"2. I'm not making everything public in the class because they don't want it that way."

Well, you said you were coverting from structs and they were easier because the members were public by default. We can only go by what you post.

Last edited by 7stud : April 1st, 2003 at 12:41 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Array of Class

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap