Create class and use methods

C++ program to create class methods and shows how to access the class methods outside of class.



Create student class, the class has 3 private attributes Rollnum, name, and percentage and also 3 methods to get name .roll number also write a function that calculates the percentage you are passing marks in that function. Test everything inside main function.
 #include<string>  
 #include<iostream>  
 using namespace std;  
 class student  
 {  
   private:  
   int rollNo;  
   double Percent;  
   string name;  
 public:  
   void GetName(string n)  //method taking name of student
   {  
     name=n;  
   }  
   void getRoll(int r){rollNo=r;}  
   double CalPre()  //method to calculate percentage
   {  
     int a,b,c,d,e;  
     cout<<"Enter your 5 subject number out of 100 :";  
     cin>>a>>b>>c>>d>>e;  
     Percent=(a+b+c+d+e)/5;  
     return Percent;  
   }  
   void print()  //method to print
   {  
     cout<<"rollNo :" <<rollNo<<endl;  
     cout<<name<<endl;  
     cout<<"Percentage of student :"<<Percent<<endl;  
   }  
 };  
 int main()  
 {  
   student s1;  
   s1.GetName("Shivam choubey");  
   s1.getRoll(1837);  
   s1.CalPre();  
   s1.print();  
 }  
Enter your 5 subject number out of 100 :55
66
99
78 
88
rollNo :1837
Shivam choubey
Percentage of student :77

0 Comments