Student details using classes and object
Aim:To write a C++ program to display the student details using classes and object as array.
Algorithm:
Step 1: Create a class as student.
Step 2: Declare the data members rollno, name, mark1, mark2, mark3, total and
average.
Step 3: Declare the member functions as getdata() and displaydata().
3.1 getdata() method used to get the student details.
3.2 displaydata() method used to display the student details.
Step 4: In the main, create an object array for the student class using the following
syntax:
Classname objectname[size];
Step 5: Get the number of students.
Step 6: In getdata() method, get the student details using for loop.
Step 7: In displaydata() method, display the student details using for loop.
Step 8: Use the following syntax to call the member functions from the main
Objectname.methodname();
Program:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class student
{
int rno;
char name[20];
int m1,m2,m3,t;
float avg;
public:
void getdata();
void displaydata();
};
void student :: getdata()
{
cout<<"Enter the roll no:";
cin>>rno;
cout<<"Enter the name:";
cin>>name;
cout<<"Enter the mark 1:";
cin>>m1;
cout<<"Enter the mark 2:";
cin>>m2;
cout<<"Enter the mark 3:";
cin>>m3;
t=m1+m2+m3; avg=t/3;
}
void student :: displaydata()
{
cout<<setw(4)<<rno;
cout<<setw(10)<<name;
cout<<setw(6)<<m1<<setw(8)<<m2<<setw(8)<<m3;
cout<<setw(6)<<t<<setw(7);
cout<<avg<<endl;
}
void main()
{
student a[10];
clrscr();
int n;
cout<<"Enter the range of the student\n";
cin>>n;
for(int i=0;i<n;i++)
{
a[i].getdata();
}
cout<<"\n\n";
cout<<"**************************************************"<<endl;
cout<<"\t"<<" Student Details: "<<endl;
cout<<"**************************************************"<<endl;
cout<<setw(5)<<"Rollno"<<setw(8)<<"Name"<<setw(8)<<"Mark1"<<setw(8)<<"Mark2"<<setw(8)<<"Mark3"<<setw(6)<<"Total"<<setw(6)<<"Avg"<<"\n";
for(int j=0;j<n;j++)
{
a[j].displaydata();
}
getch();
}
Output :
Enter the range of the student 2
Enter the roll no:1
Enter the name:mary
Enter the mark 1:89
Enter the mark 2:89
Enter the mark 3:78
Enter the roll no:2
Enter the name:jim
Enter the mark 1:67
Enter the mark 2:78
Enter the mark 3:89
Enter the roll no:2
Enter the name:jack
Enter the mark 1:78
Enter the mark 2:89
Enter the mark 3:67
**************************************************
Student Details:
**************************************************
Rollno Name Mark1 Mark2 Mark3 Total Avg
1 mary 89 89 78 256 85
2 jim 67 78 89 234 78
3 jack 78 89 67 234 78
1 comments:
its is very use full to me
and i am a webdesigner and this is my site
www.collectinfo.net
pls tell you are feed back in this site to improve my site
Post a Comment