View Single Post
Old 12-05-2006, 07:35 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,711
redhead is on a distinguished road
As this is C++ I would use classes instead of structs.. And then just overload the standard operators.
Code:
#include <iostream>
#include <string>
class student
{
  private:
    student(void){}; // Impossible to create a student without a name
  public:
    std::string name;
    int major;
    float gpa;
    student(std::string n){name=n;};
    student(std::string n, int m){name=n; major=m;};
    student(std::string n, int m, float g){name=n; major=m; gpa=g;};
    student operator= (student s){ 
      name=s.name; 
      major=s.major; 
      gpa=s.gpa;
    };
};
std::ostream &operator<<(std::ostream &os, student s){
  os << s.name << std::endl;
  os << '\t' << s.major << std::endl;
  os << '\t' << s.gpa << std::endl;
  return os;
};

int main()
{
  student s1("felix", 4, 3.9);
  student s2 = s1;
  std::cout << s1;
  std::cout << s2;
  return 0;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 12-05-2006 at 07:49 AM.
redhead is offline   Reply With Quote