Initialize the class members using constructors and destroy the objects by using destructor
Define a class Student with the following specification:
PRIVATE DATA MEMBERS:
regno integer sname 20 character s1, s2. s3 float total float
PUBLIC MEMBER FUNCTIONS:
Student() Default constructor to initialize data members, s1 = s2 = s3 = 0
Student(rn, sn, m, p, c) Parameterized constructor accept values for regno, sname, s1, s2 and s3
calculate() Function to calculate the student total marks
putdata() Function to display all the data members of student
~student() Destructor to destroy the data objects
Runtime Input :
12
Ramesh
60
70
65
Output :
12
Ramesh
60
70
65
195
#include <iostream>
using namespace std;
class Student{
int regno;
float s1, s2, s3, total;
char *sname;
public:
Student(){}
Student(int rn, char sn[20], float m, float p, float c){
sname = sn;
regno = rn;
s1 = m;
s2 = p;
s3 = c;
}
void calculate(){
total = s1 + s2 + s3;
}
void putdata(){
cout<<"regno: "<<regno<<endl;
cout<<"name: "<<sname<<endl;
cout<<"s1: "<<s1<<endl;
cout<<"s2: "<<s2<<endl;
cout<<"s3: "<<s3<<endl;
cout<<"Total: "<<total<<endl;
}
~Student(){
}
};
int main(){
int regno;
float s1, s2, s3;
string name;
char sname[20];
cout<<"Enter runtime input\n";
cin>>regno;
cin>>name;
for(int i = 0; i < name.length(); i++) sname[i] = name[i];
cin>>s1;
cin>>s2;
cin>>s3;
Student student(regno, sname, s1, s2, s3);
cout<<"output\n";
student.calculate();
student.putdata();
return 0;
}
Comments
Leave a comment