473,385 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Need help with inheritance

14
I need to complete derived class and main class:
derived class should have data member: major(string), grades: an array of integrs (size 5).
it should have methods to:
1-set and get the major.
2-insert a grade in the grades array.
3-compute the gpa of the 5 courses.
THEN:
MAIN METHOD THAT :
1-Creat a diploma student.
2-et the name of the student to a value that program gets from the user.
3-sets itt gender and age after getting them from the user.
4-sets the major a value that the user gets interactive.
5-sets 5 grades to value that user inters interactively.
6-displays the name of students his gender,his age and major.
7-computes the GPA of this student.
creates a second diploma student whoes name sami and age 21,his major math ,he has the same grade s as the fist student.Use message passing to assign grades to the second student.
8-Display the grades of the first student and second student in a form of table of two rows.

========================================
#include <iostream>
using namespace std;

class student
{
protected:
int id;
string name;
char gender;
int age;
public :

student (){name=" ";gender=' ';age=0;id=0;}
void setid(int s){id=s;}
void setName(string n){name=n;}
void setGender(char m){gender=m;}
void setAge(int g){age=g;}
int getid(){return id;}
string getName(){return name;}
char getGender(){return gender;}
int getAge(){return age;}

};

class dpstudent : public student{
protected:
string major;
int grade[5];

public:
dpstudent(){ for(int i=0;i<5;i++)grade[i]=0;major=' ';}
void setmajor(string j){major=j;}
void setGrade(int ag[]){ for(int i=0;i<5;i++) grade[i]=ag[i];}
int getGrade(int i){return grade[i];}
string getmajor(){return major;}
int computegpa(){int m=0;int gpa=0;
for(int i=0;i<5;i++) m=m+grade[i];
gpa=m/5;
return gpa;}
};
int main(){
int grade[5];
string name;
char m;
int age;
string major;
dpstudent f;
cout<<"enter the name of frist student"<<endl;
cin>>name;
f.setName("name");
cout<<"enter the gender"<<endl;
cin>>m;
f.setGender('m');
cout<<"enter the age"<<endl;
cin>>age;
f.setAge(age);
cout<<"enter the major"<<endl;
cin>>major;
f.setmajor("major");
for(int i=0;i<5;i++){
cout<<"enter the grade: ";
cin>>grade[i];
}
for( i=0;i<5;i++)
cout<<grade[i]<<endl;
cout<<"the gpa of the 5 courses is"<<f.computegpa()<<endl;
return 0;
}
Nov 30 '06 #1
4 1841
horace1
1,510 Expert 1GB
not sure what your problem is as you did not say

however, noticed a couple of things in the last few lines of your program - you did not define i in the for() and you did not call setGrade() to set up the grade array ready for computegpa(). See lines marks // ** in the code below
Expand|Select|Wrap|Line Numbers
  1. for(int i=0;i<5;i++)   //** added int
  2. cout<<grade[i]<<endl;
  3.  
  4. f.setGrade(grade);     // ** added line
  5. cout<<"the gpa of the 5 courses is"<<f.computegpa()<<endl;
  6. return 0;
  7. }
  8.  
Nov 30 '06 #2
flash
14
not sure what your problem is as you did not say

however, noticed a couple of things in the last few lines of your program - you did not define i in the for() and you did not call setGrade() to set up the grade array ready for computegpa(). See lines marks // ** in the code below
Expand|Select|Wrap|Line Numbers
  1. for(int i=0;i<5;i++)   //** added int
  2. cout<<grade[i]<<endl;
  3.  
  4. f.setGrade(grade);     // ** added line
  5. cout<<"the gpa of the 5 courses is"<<f.computegpa()<<endl;
  6. return 0;
  7. }
  8.  
Thanks
could u try this program in microsoft visual c++ 6.0 (1998).
it is not working well.
Dec 1 '06 #3
horace1
1,510 Expert 1GB
Thanks
could u try this program in microsoft visual c++ 6.0 (1998).
it is not working well.
I don't have Visual C - I ran the program using DEV-C++, Cygwin g++ and Borland CBuilder 5 OK


this is a run (input in bold)
enter the name of frist student sam
enter the gender m
enter the age 23
enter the major maths
enter the grade: 30
enter the grade: 40
enter the grade: 50
enter the grade: 60
enter the grade: 70
30
40
50
60
70
the gpa of the 5 courses is50

what happens when you run the program?
what data are you using? - note that the name must be one word, i.e. Sam not Sam Jones.

and here is the complete code
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class student
  5. {
  6. protected:
  7. int id;
  8. string name;
  9. char gender;
  10. int age;
  11. public :
  12.  
  13. student (){name=" ";gender=' ';age=0;id=0;}
  14. void setid(int s){id=s;}
  15. void setName(string n){name=n;}
  16. void setGender(char m){gender=m;}
  17. void setAge(int g){age=g;}
  18. int getid(){return id;}
  19. string getName(){return name;}
  20. char getGender(){return gender;}
  21. int getAge(){return age;}
  22.  
  23. };
  24.  
  25. class dpstudent : public student{
  26. protected:
  27. string major;
  28. int grade[5];
  29.  
  30. public:
  31. dpstudent(){ for(int i=0;i<5;i++)grade[i]=0;major=' ';}
  32. void setmajor(string j){major=j;}
  33. void setGrade(int ag[]){ for(int i=0;i<5;i++) grade[i]=ag[i];}
  34. int getGrade(int i){return grade[i];}
  35. string getmajor(){return major;}
  36. int computegpa(){int m=0;int gpa=0;
  37. for(int i=0;i<5;i++) m=m+grade[i];
  38. gpa=m/5;
  39. return gpa;}
  40. };
  41. int main(){
  42. int grade[5];
  43. string name;
  44. char m;
  45. int age;
  46. string major;
  47. dpstudent f;
  48. cout<<"enter the name of frist student"<<endl;
  49. cin>>name;
  50. f.setName("name");
  51. cout<<"enter the gender"<<endl;
  52. cin>>m;
  53. f.setGender('m');
  54. cout<<"enter the age"<<endl;
  55. cin>>age;
  56. f.setAge(age);
  57. cout<<"enter the major"<<endl;
  58. cin>>major;
  59. f.setmajor("major");
  60. for(int i=0;i<5;i++){
  61. cout<<"enter the grade: ";
  62. cin>>grade[i];
  63. }
  64. for(int i=0;i<5;i++)   //** added int
  65. cout<<grade[i]<<endl;
  66.  
  67. f.setGrade(grade);     // ** added
  68. cout<<"the gpa of the 5 courses is"<<f.computegpa()<<endl;
  69. return 0;
  70. }
  71.  
Dec 1 '06 #4
flash
14
I don't have Visual C - I ran the program using DEV-C++, Cygwin g++ and Borland CBuilder 5 OK


this is a run (input in bold)
enter the name of frist student sam
enter the gender m
enter the age 23
enter the major maths
enter the grade: 30
enter the grade: 40
enter the grade: 50
enter the grade: 60
enter the grade: 70
30
40
50
60
70
the gpa of the 5 courses is50

what happens when you run the program?
what data are you using? - note that the name must be one word, i.e. Sam not Sam Jones.

and here is the complete code
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class student
  5. {
  6. protected:
  7. int id;
  8. string name;
  9. char gender;
  10. int age;
  11. public :
  12.  
  13. student (){name=" ";gender=' ';age=0;id=0;}
  14. void setid(int s){id=s;}
  15. void setName(string n){name=n;}
  16. void setGender(char m){gender=m;}
  17. void setAge(int g){age=g;}
  18. int getid(){return id;}
  19. string getName(){return name;}
  20. char getGender(){return gender;}
  21. int getAge(){return age;}
  22.  
  23. };
  24.  
  25. class dpstudent : public student{
  26. protected:
  27. string major;
  28. int grade[5];
  29.  
  30. public:
  31. dpstudent(){ for(int i=0;i<5;i++)grade[i]=0;major=' ';}
  32. void setmajor(string j){major=j;}
  33. void setGrade(int ag[]){ for(int i=0;i<5;i++) grade[i]=ag[i];}
  34. int getGrade(int i){return grade[i];}
  35. string getmajor(){return major;}
  36. int computegpa(){int m=0;int gpa=0;
  37. for(int i=0;i<5;i++) m=m+grade[i];
  38. gpa=m/5;
  39. return gpa;}
  40. };
  41. int main(){
  42. int grade[5];
  43. string name;
  44. char m;
  45. int age;
  46. string major;
  47. dpstudent f;
  48. cout<<"enter the name of frist student"<<endl;
  49. cin>>name;
  50. f.setName("name");
  51. cout<<"enter the gender"<<endl;
  52. cin>>m;
  53. f.setGender('m');
  54. cout<<"enter the age"<<endl;
  55. cin>>age;
  56. f.setAge(age);
  57. cout<<"enter the major"<<endl;
  58. cin>>major;
  59. f.setmajor("major");
  60. for(int i=0;i<5;i++){
  61. cout<<"enter the grade: ";
  62. cin>>grade[i];
  63. }
  64. for(int i=0;i<5;i++)   //** added int
  65. cout<<grade[i]<<endl;
  66.  
  67. f.setGrade(grade);     // ** added
  68. cout<<"the gpa of the 5 courses is"<<f.computegpa()<<endl;
  69. return 0;
  70. }
  71.  




AFTER LINKING (MICROSOFT VISUAL C++):
Compiling...
asss.cpp
c:\documents and settings\m8au\desktop\assssss\asss.cpp(50) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there
is no acceptable conversion)
c:\documents and settings\m8au\desktop\assssss\asss.cpp(59) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there
is no acceptable conversion)
c:\documents and settings\m8au\desktop\assssss\asss.cpp(65) : error C2374: 'i' : redefinition; multiple initialization
c:\documents and settings\m8au\desktop\assssss\asss.cpp(61) : see declaration of 'i'
Error executing cl.exe.

asss.obj - 3 error(s), 0 warning(s)
Dec 8 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
30
by: Vla | last post by:
why did the designers of c++ think it would be more useful than it turned out to be?
17
by: Student | last post by:
Hi All, I have an assignment for my Programming language project to create a compiler that takes a C++ file as input and translate it into the C file. Here I have to take care of inheritance and...
13
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
18
by: bsruth | last post by:
I tried for an hour to find some reference to concrete information on why this particular inheritance implementation is a bad idea, but couldn't. So I'm sorry if this has been answered before....
21
by: raylopez99 | last post by:
Well, contrary to the implication in my 2000 textbook on C# (public beta version), C# does allow multiple inheritance, so long as it's serially chained as follows: class derived02 : derived01 {...
22
by: Ramon F Herrera | last post by:
My goal is to study (in the RMS sense) and familiarize myself with some OSS code, until I reach the point at which I can make non-trivial modifications to it. The class of applications I have in...
3
by: Immortal Nephi | last post by:
I write three classes. The name of class are A, B, and C. Class A and Class B are inaccessible to the client. Class C is accessible to the client. Class B has inheritance to Class A. The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.