Skip to content

Instantly share code, notes, and snippets.

@rajan9519
Last active April 19, 2020 04:48
Show Gist options
  • Select an option

  • Save rajan9519/f857a28eec5a3bf8a4a64609a196960b to your computer and use it in GitHub Desktop.

Select an option

Save rajan9519/f857a28eec5a3bf8a4a64609a196960b to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
class student
{
private:
string name;
int rollNumeber;
public:
void setName(string sName); // sets name of the student
void setRollNumber(int roll); // sets roll number of the student
string getName(); // returns name of the student
int getRollNumber(); // return the roll number of the student
};
void student::setName(string sName)
{
name = sName; // assigning sName to private member variable name
}
void student:: setRollNumber(int roll)
{
rollNumeber = roll; // assigning roll to private member variable rollNumber
}
string student ::getName()
{
return name; // return name of the student
}
int student :: getRollNumber()
{
return rollNumeber; // return rollNumber of the student
}
int main()
{
student s; // creating an instance of student class
s.setName("Rajan"); // accessing public member function setName
s.setRollNumber(62);// accessing public member function setName
cout<<"Using \" . \" to access members\n";
cout<<"Student's name is: "<<s.getName()<<"\n";
cout<<"Student's roll number is: "<<s.getRollNumber()<<"\n\n";
// Accessing pointer instance of the class
student *s2 = new student;
s2->setName("Rajan singh");
s2->setRollNumber(62);
cout<<"Using \" -> \" to access members\n";
cout<<"Student's name is: "<<s2->getName()<<"\n";
cout<<"Student's roll number is: "<<s2->getRollNumber()<<"\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment