Inheritance Class ...... help needed asap

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 41
Reputation: raj157 is an unknown quantity at this point 
Solved Threads: 0
raj157 raj157 is offline Offline
Light Poster

Inheritance Class ...... help needed asap

 
0
  #1
Apr 11th, 2007
Well it is an assignment question here is the question :

Implement a base class Person. Derive classes Student and Instructor from Person. A person has
a name and a birthday. A student has a major, and an instructor has a salary. Write the class
definitions, the constructors, and the member functions print() for all classes.

this is what i have got so far any kind of help will be good :

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Person{
  5. public:
  6. Person( string nam, string day);
  7. string get_name()const;
  8. string get_date()const;
  9. void print()const;
  10. private:
  11. string name;
  12. string date;
  13. };
  14.  
  15. Person::Person( string nam,string day){
  16. name= nam;
  17. date= day;
  18. }
  19.  
  20. string Person::get_name() const
  21. {
  22. return name;
  23. }
  24.  
  25. string Person::get_date() const
  26. {
  27. return date;
  28. }
  29.  
  30. void Person:: print()const{
  31. cout<<" Mr/Mrs."<<name<<" was born on "<<date<<"\n";}
  32.  
  33. int main (){
  34. string nam1;
  35. string dat1;
  36. cout<< " Please enter the name of student \n";
  37. cin>>nam1;
  38. cout<< " Please enter the date of birth in the format dd/mm/yyyy \n";
  39. cin>>dat1;
  40. Person(nam1,dat1);
  41. return 0;
  42. }

Well i have managed to get the base class person but it gives error can someone help me out with whats wrong and how to implement the other required derived class student and instructor
Last edited by Ancient Dragon; Apr 11th, 2007 at 8:45 am. Reason: correct code tags and reformat code so it can be read
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Inheritance Class ...... help needed asap

 
0
  #2
Apr 11th, 2007
First learn how to properly format your programs. I helped you out this time by doing it for you just so you can see how it should be done. There is absolutely nothing wrong with being generous with spaces and line feeds -- makes your program a lot easier to read.

>> it gives error
what error? I suspect it might be because you did not include <string> header file.

>>and how to implement the other required derived class student and instructor
here is how to declare Student. Other derived classes are the same format.
  1. class Student : public Person
  2. {
  3. // your code here
  4. };
Last edited by Ancient Dragon; Apr 11th, 2007 at 8:50 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 41
Reputation: raj157 is an unknown quantity at this point 
Solved Threads: 0
raj157 raj157 is offline Offline
Light Poster

Re: Inheritance Class ...... help needed asap

 
0
  #3
Apr 11th, 2007
well i did tht ... still does not work .. gives error of one or multiple defined symbols found .....
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Inheritance Class ...... help needed asap

 
0
  #4
Apr 11th, 2007
post code please.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 41
Reputation: raj157 is an unknown quantity at this point 
Solved Threads: 0
raj157 raj157 is offline Offline
Light Poster

Re: Inheritance Class ...... help needed asap

 
0
  #5
Apr 11th, 2007
#include <iostream>
#include <string>
using namespace std;

class Person{
public:
Person( string nam, string day);
string get_name()const;
string get_date()const;
void print()const;
private:
string name;
string date;
};

Person:erson( string nam,string day){
name= nam;
date= day;
}

string Person::get_name() const {return name;}
string Person::get_date() const {return date;}

void Person:: print()const{
cout<<" Mr/Mrs."<<name<<" was born on "<<date<<"\n";}

int main (){
string nam1;string dat1;
cout<< " Please enter the name of student \n";
cin>>nam1;
cout<< " Please enter the date of birth in the format dd/mm/yyyy \n";
cin>>dat1;
Person(nam1,dat1);
return 0;
}

Last edited by raj157; Apr 11th, 2007 at 12:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 41
Reputation: raj157 is an unknown quantity at this point 
Solved Threads: 0
raj157 raj157 is offline Offline
Light Poster

Re: Inheritance Class ...... help needed asap

 
0
  #6
Apr 11th, 2007
well i got it running but now i cant get it to print the output .. can anyone help me asap... thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,608
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 463
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Inheritance Class ...... help needed asap

 
0
  #7
Apr 11th, 2007
You need to call the print function after creating the object. Something along the lines of obj.print().
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 41
Reputation: raj157 is an unknown quantity at this point 
Solved Threads: 0
raj157 raj157 is offline Offline
Light Poster

Re: Inheritance Class ...... help needed asap

 
0
  #8
Apr 11th, 2007
dint learn yet about object.print ... have only learned about the print in the class like i have done in my code.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,608
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 463
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Inheritance Class ...... help needed asap

 
0
  #9
Apr 11th, 2007
I meant, do something like:
  1. Person obj(nam1, dat1);
  2. obj.print(); // call the member function print
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 41
Reputation: raj157 is an unknown quantity at this point 
Solved Threads: 0
raj157 raj157 is offline Offline
Light Poster

Re: Inheritance Class ...... help needed asap

 
0
  #10
Apr 11th, 2007
i am confussd in how to call the print function. lets say the class name is Person then how would u print it .... i mean what command would u type
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC