Monday, 25 March 2019

CPP-EX65

Create a C++ class for student having following members-Rollno,Name,No. of subjects,Marks of each subject (no. of subjects varies for each student) Write a parameterized constructor which initialises Rollno,Name & no. of Subjects & creates the array of marks dynamically.Display the details of all  students with percentage & class obtained

 #include<iostream.h>

#include<conio.h>

#include<malloc.h>

class student

{

int rollno;

char name[10];

int tot_sub;

char sub_name[10][30];

int marks[10];

int tot_marks;

float per;

//    char grade;

public:

void getdata()

{

cout<<"\n\n Enter the Roll no: ";

cin>>rollno;

cout<<"\n\n Enter the name: ";

cin>>name;

cout<<"\n\n How many subject: ";

cin>>tot_sub;

 for(int i=0;i<tot_sub;i++)

{

cout<<"\n\n Enter the subject name; ";

cin>>sub_name[i];

cout<<"\n\n Enter the marks: ";

cin>>marks[i];

}

}

public:void display()

{

tot_marks=0;

cout<<"\n Roll number:"<<rollno;

cout<<"\n Student name: "<<name;

for(int i=0;i<tot_sub;i++)

{

cout<<"\n Subject name: "<<sub_name[i];

cout<<"\n Subject mark: "<<marks[i];

tot_marks=tot_marks+marks[i];

}

per=tot_marks/tot_sub;

cout<<"\n\n Total obtain marks: "<<tot_marks;

cout<<"\n\n Percentge: "<<per;

if(per>=70)

cout<<"\n \n Grade=Dist";

else if(per>=60)

cout<<"\n \n Grade=A";

else if(per>=50)

cout<<"\n \n Grade=B";

else if(per>=40)

cout<<"\n \n Grade=Pass";

else

cout<<"\n \n Grade=Fail";

}

};

 void main()

{

clrscr();

student s;

s.getdata();

s.display();

getch();

}

 Another Way

 #include<conio.h>

#include<iostream.h>

#include<string.h>

class student

{

int rollno;

char name[10];

int subject;

int *marks;

public:

student()

{

}

student(int rno,char n[],int s);

void display();

};

 student::student(int rno,char n[10],int s)

{

int i;

rollno=rno;

strcpy(name,n);

subject=s;

marks=new int[s];

for(i=0;i<s;i++)

{

cout<<"enter marks m"<<i+1<<" =";

cin>>marks[i];

}

}

 void student::display()

{

int i,n,total=0;

float per;

char class1[10];

cout<<"\n roll number="<<rollno;

cout<<"\n name ="<<name;

cout<<"\n number of subject="<<subject;

for(i=0;i<subject;i++)

total=total+marks[i];

per=total/subject;

if(per>70)

strcpy(class1,"first");

else

if(per>60)

strcpy(class1,"higher second");

else

if(per>50)

strcpy(class1,"second");

else

if(per>40)

strcpy(class1,"pass");

else

strcpy(class1,"fail");

cout<<"\n percentsge="<<per;

cout<<"\n class obtained="<<class1;

}

 int main()

{

student s1[10];

int rno,s,num,i;

char n[10];

clrscr();

cout<<"\n enter how many student :";

cin>>num;

cout<<"\n enter student details";

for(i=0;i<num;i++)

{

cout<<"\n enter rollnumber =";

cin>>rno;

cout<<"enter name =";

cin>>n;

cout<<"enter how many subject =";

cin>>s;

student s2(rno,n,s);

s1[i]=s2;

}

cout<<"Details of all student ";

for(i=0;i<num;i++)

s1[i].display();

getch();

 return 0;  }