C++ oop programming . Need Help

Can anyone do it in easiest way ?
Write a c++ program to create a base class called STUDENT (name,roll number,age) and using inheritance create classes UG student and PG student having fields as semester fees and stipend. Enter the data of 6 students of each class. Find average age semester wise for all UG and PG students separately.
Note.
Assume at least two different values for semester field for each of UG and PG classes.
UG=undergraduate
PG=postgraduate
Field means data members..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include<iostream>
using namespace std;
#include<conio.h>
class Student
{
	protected:
		char name [20];
		int rn,age;
};

class Ug : public Student
{
	int sem,fee,sti;

	public:
	void getdata()
	{
		cout<<"\nEnter Roll Number : " ;
		cin>>rn;
		cout<<"\nEnter Name: ";
		cin>>name;
		cout<<"\nEnter Age : ";
		cin>>age;
		cout<<"\nEnter Semester : ";
		cin>>sem;
		cout<<"\nEnter Fee : ";
		cin>>fee;
		cout<<"\nEnter Stipend : ";
		cin>>sti;
	}

void display()
	{
		cout<<name<<"\t"<<age<<"\t"<<sem<<"\n";
	}

	int getsem()
	{
		return(sem);
	}

	int getage()
	{
		return(age);
	}

};

class Pg : public Student
{
	int sem,fee,sti;

	public:
	void getdata()
	{
		cout<<"\nEnter Roll Number : " ;
		cin>>rn;
		cout<<"\nEnter Name: ";
		cin>>name;
		cout<<"\nEnter Age : ";
		cin>>age;
		cout<<"\nEnter Semester : ";
		cin>>sem;
		cout<<"\nEnter Fee : ";
		cin>>fee;
		cout<<"\nEnter Stipend : ";
		cin>>sti;
	}

	void display()
	{
		cout<<name<<"\t"<<age<<"\t"<<sem<<"\n";
	}

	int getsem()
	{
		return(sem);
	}

	int getage()
	{
		return(age);
	}

};

int main()
{
	

	Pg pg[6];
	Ug ug[6];

	int flag,age,sem,agesum=0,semcnt=0,i,j;

	cout<<"\n\nEnter Post Graduate students details..\n";
	for(i=0; i<6; i++ )
		pg[i].getdata();

	cout<<"\n\nEnter Under Graduate students details..\n";
	for(i=0; i<6; i++ )
		ug[i].getdata();

	cout<<"\nUG - students details..\n";
	cout<<"--------------------------\n";
	cout<<"Name\tAge\tSem\n";
	for(i=0; i<6; i++ )
		ug[i].display();
	cout<<"--------------------------\n";
	for(i=1; i<9; i++ )
	{
		flag=0;
		for(j=0; j<6; j++ )
		{
			sem=ug[j].getsem();
			age=ug[j].getage();

			if( i==sem )
			{
				agesum+=age;
				semcnt++;
				flag=1;
			}
		}
	if(flag)
	   cout<<i<<"\t"<<agesum/semcnt<<endl;
}

	agesum=0,semcnt=0;

	cout<<"\n\nPG - students details..\n";
	cout<<"--------------------------\n";
	cout<<"Name\tAge\tSem\n";
	for(i=0; i<6; i++ )
		pg[i].display();
	cout<<"--------------------------\n";
	for(i=1; i<9; i++ )
	{
		flag=0;
		for(j=0; j<6; j++ )
		{
			sem=pg[j].getsem();
			age=pg[j].getage();

			if( i==sem )
			{
				agesum+=age;
				semcnt++;
				flag=1;
			}
		}
			if(flag)
			cout<<i<<"\t"<<agesum/semcnt<<endl;
	}

	getch();
	return 0;

}
Last edited on
Yes, but that isn't helping you.
I'll take some concept from it you can try !!!!
That's not how it works. Someone can help you with your code, but we don't write it for you (unless some enthusiastic soul has nothing better to do).
this is little bit complex can you make is simple now ?
Write a c++ program to create a base class called STUDENT ...
1
2
3
4
5
6
class Student
{
	protected:
		char name [20];
		int rn,age;
};


That isn't an object. It's a record, or in C speak, a struct.

An object is something that you can only communicate with using messages. In C++ that is, by calling member methods. You cannot access object data directly. This is why that isn't an object.

To make it an object, you need to provide a set of methods that the object will support, and implement them. Object methods are virtual functions in C++.

Plus, C++ requires you to consider initialization, copy and destruction too.

Object oriented programming in C++ only works when object methods are implemented with virtual functions and the objects themselves are accessed thru inderection; that is, thru a pointer or reference.

It is not normal for object methods to ask for user input for the application. For example, consiter string::clear(). What do you think would happen if that method prompted for verification first? You'd have this prompt every time it was used. But clearly, verification has little to do with actually clearing the string, so the prompt isn't part ot it. And it shouldn't be part of your methods also.

Any functions (or portions of functions) that are common in derived classes that only use members of the base class should be in the base class.

In other words, these liness:

1
2
3
4
5
6
		cout<<"\nEnter Roll Number : " ;
		cin>>rn;
		cout<<"\nEnter Name: ";
		cin>>name;
		cout<<"\nEnter Age : ";
		cin>>age;


should be in a function in the base class rather than be duplicated in both derived classes. Similarly, getAge() and the first part of display() should be a base class functions.

That's good idea ! Thanks
Actually I would argue that those lines should not be part of this class at all. IMO, you should separate the User Interface from the program logic.

Write a c++ program to create a base class called STUDENT (name,roll number,age) and using inheritance create classes UG student and PG student having fields as semester fees and stipend.

I interpreted this to mean something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Student {
    std::string name;
    int rollNumber;
    unsigned age;
};

// Undergrads pay a fee
class UnderGrad : public Student {
    double semesterFees;
};

// Postgrads are paid a stipend
class PostGrad : public Student {
    double stipend;
};


I think this makes more sense than your interpretation. If both UG and PG have the same fields then there would be no need for 3 separate classes: they'd all go in one class.
Topic archived. No new replies allowed.