The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Page 2 -
Dynamic Object Name
Page 2 - Discuss Dynamic Object Name in the C Programming forum on Dev Shed. Dynamic Object Name C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 9th, 2003, 05:12 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
FWIW, here's my code, tested with g++ 3.2 on a laptop running RH Linux 8.0. AFAIK it is standard C++ and should work on VC++ with no problems too (can someone verify this for me please, because I can only test it for myself on monday at work - thanks in advance).
Code:
#include <iostream>
using namespace std;
class Student {
public:
int x, y;
Student(int a, int b);
};
Student::Student(int a, int b) {
x = a; y = b;
}
int main(void) {
int num_students;
cout << "Enter # of students: ";
cin >> num_students;
Student *student = new Student[num_students](5, 3);
for (int i = 0; i < num_students; i++) {
cout << "Student #" << i+1 << ". " << student[i].x << ", " << student[i].y << endl;
}
delete [] student;
return 0;
}
Thanks for the vote of confidence 7stud 
|

March 9th, 2003, 05:30 PM
|
 |
Contributing User
|
|
Join Date: Jul 2002
Location: London
Posts: 349
Time spent in forums: 4 h 19 m 21 sec
Reputation Power: 11
|
|
Errors on VC++ 6.0 matey
Code:
Student *student = new Student[num_students](5, 3);
C:\Program Files\Microsoft Visual Studio\MyProjects\test\test.cpp(22) : error C2538: new : cannot specify initializer for arrays
C:\Program Files\Microsoft Visual Studio\MyProjects\test\test.cpp(22) : error C2512: 'Student' : no appropriate default constructor available
|

March 9th, 2003, 05:37 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
Aw shoot, guess I'd have to declare a constructor with no arguments to make it work with VC++ 6.0. Guess this should work then:
Code:
#include <iostream>
using namespace std;
class Student {
public:
int x, y;
Student();
Student(int a, int b);
};
Student::Student() {
x = 5; y = 3;
}
Student::Student(int a, int b) {
x = a; y = b;
}
int main(void) {
int num_students;
cout << "Enter # of students: ";
cin >> num_students;
Student *student = new Student[num_students];
for (int i = 0; i < num_students; i++) {
cout << "Student #" << i+1 << ". " << student[i].x << ", " << student[i].y << endl;
}
delete [] student;
return 0;
}
Thanks for testing it out 
|

March 9th, 2003, 05:47 PM
|
 |
Contributing User
|
|
Join Date: Jul 2002
Location: London
Posts: 349
Time spent in forums: 4 h 19 m 21 sec
Reputation Power: 11
|
|
|
Yep that works a treat Scorps m8
|

March 9th, 2003, 07:16 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
Gmorphus,
My apologies. I didn't notice the constructor issue if that's what you were questioning.
Last edited by 7stud : March 9th, 2003 at 07:21 PM.
|

March 12th, 2003, 12:38 PM
|
|
Contributing User
|
|
Join Date: Sep 2001
Location: ISRAEL
Posts: 35
Time spent in forums: < 1 sec
Reputation Power: 12
|
|
apologies accepted
scorp, that is quite exactly what i meant. but, you will have to add Create(x,y) function to intialize x and y, and to retrieve the info from the user.
you will also have to do it in your g++ version. what you have done there is to initalize ALL the x and y's with 3 and 5...
i don't think that how it should be...
anyway, i think we have covered this thread all around.
__________________
"Gravitation can NOT be responsible for people falling in Love"
(one of the most significant characters in the history, can you guess?)
Gmorph.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|