944,193 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3192
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 11th, 2007
0

Inheritance Class ...... help needed asap

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Light Poster
raj157 is offline Offline
41 posts
since Mar 2007
Apr 11th, 2007
0

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

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.
C++ Syntax (Toggle Plain Text)
  1. class Student : public Person
  2. {
  3. // your code here
  4. };
Last edited by Ancient Dragon; Apr 11th, 2007 at 8:50 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Apr 11th, 2007
0

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

well i did tht ... still does not work .. gives error of one or multiple defined symbols found .....
Reputation Points: 10
Solved Threads: 0
Light Poster
raj157 is offline Offline
41 posts
since Mar 2007
Apr 11th, 2007
0

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

post code please.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Apr 11th, 2007
0

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

#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.
Reputation Points: 10
Solved Threads: 0
Light Poster
raj157 is offline Offline
41 posts
since Mar 2007
Apr 11th, 2007
0

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

well i got it running but now i cant get it to print the output .. can anyone help me asap... thanks
Reputation Points: 10
Solved Threads: 0
Light Poster
raj157 is offline Offline
41 posts
since Mar 2007
Apr 11th, 2007
0

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

You need to call the print function after creating the object. Something along the lines of obj.print().
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Apr 11th, 2007
0

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

dint learn yet about object.print ... have only learned about the print in the class like i have done in my code.
Reputation Points: 10
Solved Threads: 0
Light Poster
raj157 is offline Offline
41 posts
since Mar 2007
Apr 11th, 2007
0

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

I meant, do something like:
C++ Syntax (Toggle Plain Text)
  1. Person obj(nam1, dat1);
  2. obj.print(); // call the member function print
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Apr 11th, 2007
0

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

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
Reputation Points: 10
Solved Threads: 0
Light Poster
raj157 is offline Offline
41 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Memory Question
Next Thread in C++ Forum Timeline: transform char to integers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC