Student Scope Resolution Operator Program in OOP/C++
Student Scope Resolution Operator in OOP/C++
The :: (scope resolution) operator is used to get hidden names due to variable scopes so that you can still use them. The scope resolution operator can be used as both unary and binary. You can use the unary scope operator if a namespace scope or global scope name is hidden by a particular declaration of an equivalent name during a block or class. For example, if you have a global variable of name my_var and a local variable of name my_var, to access global my_var, you'll need to use the scope resolution operator.
For Example:
#include<iostream>
using namespace std;
class Person{
private:
string Name;
string Gender;
int Age;
char Exam;
public:
Person()
{
cout<<"Name : ";
cin>>Name;
cout<<"Gender : ";
cin>>Gender;
cout<<"Age : ";
cin>>Age;
}
void setData()
{
cout<<"Name : "<<Name<<endl;
cout<<"Gender : "<<Gender<<endl;
cout<<"Age : "<<Age<<endl;
}
void getData()
{
Person::setData ();
}
};
class Student:public Person
{
private:
int RollNo;
string Subject;
public:
Student()
{
cout<<"Roll Number : ";
cin>>RollNo;
cout<<"Subject : ";
cin>>Subject;
}
void setData(){
cout<<"Roll Number: "<<RollNo<<endl;
cout<<"Subject : "<<Subject<<endl;
}
void getData(){
cout << "The roll number is: " << RollNo << "\n";
cout << "The subject is: " << Subject << "\n";
}
};
class Exam:public Student{
private:
int Sub1Marks;
int Sub2Marks;
public:
Exam()
{
cout<<"Marks scored in Subject 1 <Max:100>: ";
cin>>Sub1Marks;
cout<<"Marks scored in Subject 2 <Max:100>: ";
cin>>Sub2Marks;
}
void setData()
{
Student::getData();
if(Sub1Marks>=0 && Sub1Marks<=100 && Sub2Marks>=0 && Sub2Marks<=100)
{
cout<<"Marks scored in Subject 1 "<<Sub1Marks<<endl;
cout<<"Marks scored in Subject 2 "<<Sub2Marks<<endl;
cout<<"Total Marks Scored :"<<Sub1Marks+Sub2Marks<<endl;
}
else
{ //will show this error if marks are less than 0 and greater than 100.
cout<<"\t\tError: Marks should in between 0 to 100";
}
}
void getData()
{
Exam::setData();
}
};
main()
{
cout<<"Enter data for Student ......."<<endl;
Exam e;
cout<<"\n\nStudent detail ........"<<endl;
e.getData();
}
Comments
Post a Comment