22. Create a C++ class for a student object with the following attributes— roll no, name, number of subjects , marks of subjects. The number of subjects varies for each student. Write a parameterized constructor which initializes roll number, name and number of subject and creates the array for marks dynamically, write member function for accepting marks and display all information of student.


#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
int rno,ns;
char name[20];
int *m;
public:
student(int no,char *nm,int nos)
{
rno=no;
strcpy(name,nm);
ns=nos;
m=new int[ns];
}
void get();
void disp();
};
void student::get()
{
for(int i=0;i<ns;i++)
{
cout<<“\n\nEnter the subject “<<i+1<<” Marks” ;
cin>>m[i];
}
}
void student::disp()
{
cout<<“\nStudent Information: \n”;
cout<<“\n\nRoll no: “<<rno;
cout<<“\n\nName: “<<name;
for(int i=0;i<ns;i++)
{
cout<<“\n\nMarks of “<<i+1<<” Subject: “<<m[i];
}
}
void main()
{
int no,nos;
char nm[20];
clrscr();
cout<<“\nEnter roll no name no of subject:\n”;
cin>>no>>nm>>nos;
student st(no,nm,nos);
st.get();
st.disp();
getch();
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s