C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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:
  #16  
Old March 9th, 2003, 05:12 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,589 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 26 m 52 sec
Reputation Power: 1001
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

Reply With Quote
  #17  
Old March 9th, 2003, 05:30 PM
rendy's Avatar
rendy rendy is offline
Moderator
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: London
Posts: 348 rendy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 21 m 17 sec
Reputation Power: 7
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

Reply With Quote
  #18  
Old March 9th, 2003, 05:37 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,589 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 26 m 52 sec
Reputation Power: 1001
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

Reply With Quote
  #19  
Old March 9th, 2003, 05:47 PM
rendy's Avatar
rendy rendy is offline
Moderator
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: London
Posts: 348 rendy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 21 m 17 sec
Reputation Power: 7
Yep that works a treat Scorps m8

Reply With Quote
  #20  
Old March 9th, 2003, 07:16 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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.

Reply With Quote
  #21  
Old March 12th, 2003, 12:38 PM
Gmorphus Gmorphus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: ISRAEL
Posts: 35 Gmorphus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to Gmorphus
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Dynamic Object Name


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT