|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
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[]); |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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]; |
|
#4
|
|||
|
|||
|
What if I defined a 2nd class with the first class as a datamember?
|
|
#5
|
||||
|
||||
|
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 ![]() |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
Quote:
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 |
|
#8
|
||||
|
||||
|
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).
__________________
Jason Doucette / Xona.com™ - Programming Windows Errata Addendum "Discussion is an exchange of knowledge; argument is an exchange of ignorance." |
|
#9
|
|||
|
|||
|
"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. |
|
#10
|
|||
|
|||
|
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. |
|
#11
|
|||
|
|||
|
Quote:
That is actually what I was thinking also. |
|
#12
|
||||
|
||||
|
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. |
|
#13
|
|||
|
|||
|
"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. |
|
#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
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. |
|
#15
|
|||
|
|||
|
"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. |