C++ program to define a class and method are defined outside the class definition.
Using the same example that we use to define a class with the method. In this example, class are defined outside to class definition.
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)
{
name=n;
}
void getRoll(int );
double CalPre();
void print();
};
void student::getRoll(int r)
{rollNo=r;}
double student::CalPre()
{
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 student ::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
If you have any doubt and suggestion Please let me know