Pranav Pankaj 18107024 Oops - Lab.record

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

LAB RECORD – OBJECT ORIENTED

PROGRAMMING WITH C++


SUBMITTED BY –

PRANAV PANKAJ

ROLL NUMBER: 18107024

​INFORMATION TECHNOLOGY (B.Tech)

3​rd​ YEAR (5​th​ SEMESTER)

SUBMITTED TO –
Dr. RAJESH MAHULE
DEPARTMENT OF INFORMATION TECHNOLOGY,

SCHOOL OF STUDIES IN ENGINEERING & TECHNOLOGY

GURU GHASIDAS VISHWAVIDYALAYA, BILASPUR (C.G)


(Established under Central Universities Act 2009, No. 25 of 2009)

JULY – DECEMBER 2020


1.C++ Program to calculate Average of 5 subjects and find
percentage

#include<iostream>
using namespace std;

int main()
{
int mark[5], i;
float sum=0;
cout<<"\nEnter marks obtained in Physics, Chemistry, Maths, CS, English :: \n";
for(i=0; i<5; i++)
{
cout<<"\nEnter mark[ "<<i+1<<" ] :: ";
cin>>mark[i];
sum=sum+mark[i];
}

float avg=sum/5;
float perc;
perc=(sum/500)*100;
cout<<"\nAverage Marks of 5 Subjects = [ "<<avg<<" ] \n";
cout<<"\nPercentage in 5 Subjects = [ "<<perc<<"% ] \n";

return 0;

OUTPUT:
Enter marks obtained in Physics, Chemistry, Maths, CS, English ::

Enter mark[ 1 ] :: 79

Enter mark[ 2 ] :: 87

Enter mark[ 3 ] :: 67

Enter mark[ 4 ] :: 99

Enter mark[ 5 ] :: 80

Average Marks of 5 Subjects = [ 82.4 ]


Percentage in 5 Subjects = [ 82.4% ]

2.WAP using a cout object.

#include <iostream>
using namespace std;
int main( ) {
char ary[] = "Welcome to C++ tutorial";
cout << "Value of ary is: " << ary << endl;
}
Output:

Value of ary is: Welcome to C++ tutorial

3.WAP to calculate compound interest.


#include <iostream>

using namespace std;

int main () {

float p, r, t, ci(1);

cout<<"Enter principal amount, rate of interest and years to evaluate CI: ";

cin>>p>>r>>t;

for (int i(0); i < t ; i++) {

ci *= (100 + r)/100;

ci *= p;

ci -= p;

cout<<"Compound interest: Rs. "<<ci<<" /-";

return 0;
}

OUTPUT:

Enter principal amount, rate of interest and years to evaluate CI: 200 40 2

Compound interest: Rs. 192 /-

4. WAP to calculate factorial of a number


#include <iostream>
using namespace std;

int main()
{
unsigned int n;
unsigned long long factorial = 1;

cout << "Enter a positive integer: ";


cin >> n;

for(int i = 1; i <=n; ++i)


{
factorial *= i;
}

cout << "Factorial of " << n << " = " << factorial;
return 0;
}
OUTPUT:
Enter a positive integer: 12
Factorial of 12 = 479001600

5.WAP to display fibonacci sequence upto given length.


#include <iostream>
using namespace std;

int main()
{
int n, t1 = 0, t2 = 1, nextTerm = 0;

cout << "Enter the number of terms: ";


cin >> n;

cout << "Fibonacci Series: ";

for (int i = 1; i <= n; ++i)


{
// Prints the first two terms.
if(i == 1)
{
cout << " " << t1;
continue;
}
if(i == 2)
{
cout << t2 << " ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;

cout << nextTerm << " ";


}
return 0;
}

OUTPUT:
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

6.WAP to calculate a^b

#include "iostream"

using namespace std;

int main () {
int a, b;
long pow(1);

cout<<"Enter the values (a, b): ";


cin>>a>>b;

for (int i(0); i < b ; i++) {


pow *= a;
}

cout<<a<<"^"<<b<<" :"<<pow;

return 0;
}
OUTPUT:
Enter the values (a, b): 10 5
10^5 :100000

7.WAP to calculate factorial of a number using function.


#include "iostream"

using namespace std;

long fact (int n) {


long f(1);

for (int i(1); i <= n ; i++) {


f *= i;
}
return f;
}

int main () {
int n;

cout<<"Enter any number to get it's factorial: ";


cin>>n;

cout<<"Factorial of the number ("<<n<<"!): "<<fact(n);


return 0;
}
OUTPUT:
Enter any number to get it's factorial: 5
Factorial of the number (5!): 120

8.WAP to swap two numbers using function


#include <iostream>
using namespace std;

int main()
{
int a = 5, b = 10, temp;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;

temp = a;
a = b;
b = temp;

cout << "\nAfter swapping." << endl;


cout << "a = " << a << ", b = " << b << endl;

return 0;
}

OUTPUT:
Before swapping.
a = 5, b = 10

After swapping.
a = 10, b = 5
9.WAP using inline function.
#include <iostream>
using namespace std;
inline int cube(int s)
{
return s*s*s;
}
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;

OUTPUT:
The cube of 3 is: 27

10.WAP shows use of return by reference.


#include "iostream"
using namespace std;

int& returnValue(int& x) {
return x;
}

int main() {
int a = 20;
int &b = returnValue(a);
cout<<"a = "<<a<<" The address of a is "<<&a<< endl;

cout<<"b = "<<b<<" The address of b is "<<&b<< endl;

returnValue(a) = 13;

cout<<"a = "<<a<< " The address of a is "<<&a<< endl;


return 0;}
OUTPUT:
a = 20 The address of a is 0x61fe14
b = 20 The address of b is 0x61fe14
a = 13 The address of a is 0x61fe14

11.WAP shows definition outside class


#include "iostream"

using namespace std;

class Test {
int a, b;

public:
Test() {}

void setter(int _a = 0, int _b = 0);


void getter();
};

void Test::setter (int _a, int _b) {


a = _a;
b = _b;
}

void Test::getter () {
cout<<"a: "<<a<<" b: "<<b;
}

int main () {
Test t;

t.setter(3, 5);
t.getter();
return 0;
}
OUTPUT:
a: 3 b: 5

12.Create 2 classes DM and DB to store distance in 2 different units that are


meters and feet. Add functions to read the 2 values values from user in 2 different
units and display the sum in a single unit.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>

class DM
{
public:
double meter,centimeter;
};

class DB
{
public:
double feet,inches;
friend void add(DM,DB);
};

void add(DM dm,DB db)


{
double d1,d2;
cout<<"\nEnter the distance in meter and entimeter:";
cin>>dm.meter>>dm.centimeter;
cout<<"\nEnter the distance in feet and inches:";
cin>>db.feet>>db.inches;
d1=dm.meter+(db.feet)/3.281;
d2=dm.centimeter+(db.inches)*2.54;
cout<<"\nMeter + Feet = "<<d1<<" meter";
cout<<"\nCentimeter + inches = "<<d2<<" cemtimeter";
}
void main()
{
clrscr();
DM dm;
DB db;
add(dm,db);
getch();
}
OUTPUT:
Enter distance in metre and centimeter: 10 12
Enter distance in feet and inches:5 4
Metre+inches=11.52323926 metre
centimetre+inches=22.16centimeter
13​.​C++ program to create class to read and add two times​.
#include <iostream>
using namespace std;

class Time
{
private:
int hours;
int minutes;
int seconds;

public:
void getTime(void);
void putTime(void);
void addTime(Time T1,Time T2);
};

void Time::getTime(void)
{
cout << "Enter time:" << endl;
cout << "Hours? "; cin>>hours;
cout << "Minutes? "; cin>>minutes;
cout << "Seconds? "; cin>>seconds;
}

void Time::putTime(void)
{
cout << endl;
cout << "Time after add: ";
cout << hours << ":" << minutes << ":" << seconds << endl;
}

void Time::addTime(Time T1,Time T2)


{

this->seconds=T1.seconds+T2.seconds;
this->minutes=T1.minutes+T2.minutes + this->seconds/60;;
this->hours= T1.hours+T2.hours + (this->minutes/60);
this->minutes %=60;
this->seconds %=60;
}

int main()
{
Time T1,T2,T3;
T1.getTime();
T2.getTime();
//add two times
T3.addTime(T1,T2);
T3.putTime();

return 0;
}
OUTPUT:
Enter time:
Hours? 22
Minutes? 44
Seconds? 55
Enter time:
Hours? 12
Minutes? 50
Seconds? 45
Time after add: 35:35:40
14.Create a class Student with a string variable 'name' and an integer
variable 'roll no.'. Assign the value of roll no as '2' and name as 'John' by
creating an object of the class;
#include "iostream"

using namespace std;

class Student {
int rno;
string name;

public:
Student(string _name, int _rno): name(_name), rno(_rno) {}

void print() {
cout<<"Student\nRoll No. : "<<this->rno<<"\nName: "<<this->name<<endl<<endl;
}
~Student() {}
};

int main () {
Student s("John", 2);

s.print();
return 0;
}
OUTPUT:
Student
Roll No. : 2
Name: John
15.Write a program to print area of rectangle by creating a class named
Area which have two functions as setDim which takes length and breadth
of the rect as parameters and getArea which returns the area of the
rectangle.

#include "iostream"

using namespace std;

//global id;
int id(1);

class Area {
float l, b;

public:
Area() {}

void setDim(int length, int breadth) {


l = length;
b = breadth;
}

float getArea () {
return l*b;
}

~Area() {}
};
int main () {
Area r;
float l, b;

cout<<"Enter dimensions of the rectangle: ";


cin>>l>>b;
r.setDim(l, b);

cout<<"\nArea of rectangle : "<<r.getArea();


return 0;
}
OUTPUT:
Enter dimensions of the rectangle: 5 6

Area of rectangle : 30
16.Write a program that would print the info (name, year of joining, salary,
address) of 3 employees by creating a class named Employee as the table.

Name Year of joining Address


Ramesh 1994 64C-New Delhi
Sam 2000 68D-Bilaspur
John 1999 26B-banglore

#include "iostream"

using namespace std;

//global id;
int id(1);

class Emp {
string name, yoj, address;

public:
Emp() {}

void feed (string _name, string _yoj, string _address) {


name = _name;
yoj = _yoj;
address = _address;
}
void fetch () {
cout<<name<<"\t"<<yoj<<"\t"<<address<<endl<<endl;
}

~Emp() {}
};

int main () {
Emp e[3];

cout<<"Enter details of 3 employees: \n";

for (int i(0); i < 3; i++) {


string nm, yr, ad;

cout<<"Employee 01 (Name, year of joining, address): ";


cin>>nm>>yr>>ad;
e[i].feed(nm, yr, ad);
}

cout<<"\n\nDetails of employees:\nName\tYear of joining\tAddress\n\n";


for (int i(0); i < 3; i++)
e[i].fetch();
return 0;
}
OUTPUT:
17. WAP to intialise an objet with another using copy constructor
- WAP to illustrate creation and desctruction of objects
- WAP to illustrate pointer to member and pointer to object concepts of
OOP.
#include "iostream"

using namespace std;

class A {
int a, b;

public:
A(): a(0), b(0) {
cout<<"Object created.\n";
}
A(int a1, int b1): a(a1), b(b1) {
cout<<"Object created using parameterised constructor.\n";
}
A(A &temp): a(temp.a), b(temp.b) {
cout<<"Object created using copy constructor.\n";
}
void display() {
cout<<"Value of a: "<<a<<endl;
cout<<"Value of b: "<<b<<endl;
}
~A() {
cout<<"Object destroyed.\n";
}
};

int main () {
// intialising using parameterised constructor
A g(1, 2);

// intialising using copy constructor


A h(g);

// displaying object values


cout<<"Value of g: ";
g.display();
cout<<"Value of h: ";
h.display();
// using pointer to member
A* ptr(&g);
ptr->display();

// using pointer to object


// but unable to access object 'cos it's a private member
// ptr->a;
return 0;}
OUTPUT:

18.WAP to show the highest scorer in a test out of 3 students who


appeared in an exam. Use 'this' pointer.
#include "iostream"

using namespace std;

class Mark {
int marks;

public:
Mark (): marks(0) {}
Mark (int _marks): marks(_marks) {}
Mark (Mark &m): marks(m.marks) {}

Mark greatest(Mark &M2, Mark &M3) {


int m1 = this->marks, m2 = M2.marks, m3 = M3.marks;
if (m1 > m2 && m1 > m3)
return *this;
else if (m2 > m3 && m2 > m1)
return M2;
else
return M3;
}
friend ostream& operator << (ostream &out, Mark const &m) {
out<<"Marks: "<<m.marks<<endl;
return out;
}
};

int main () {
Mark s1(80), s2(34), s3(79);
cout<<s1.greatest(s2, s3);
return 0;
}
OUTPUT:
MARKS:80
19.Create two objects of a class with two integer type members. Compare
the two operators have same values by overloading "==" operator.
#include "iostream"

using namespace std;

class Coor {
int x, y;

public:
Coor(): x(0), y(0) {}
Coor(int _x, int _y): x(_x), y(_y) {}
Coor(Coor &c): x(c.x), y(c.y) {}

bool operator == (Coor const &c) {


return (c.x == x && c.y == y);
}

friend ostream & operator << (ostream &out, Coor const &c) {
out<<"( "<<c.x<<", "<<c.y<<" )";
return out;
}
~Coor() {}
};

int main () {
Coor p1, p2;

int a1, b1, a2, b2;

cout<<"Enter coordinates of first point: ";


cin>>a1>>b1;

cout<<"Enter coordinates of second point: ";


cin>>a2>>b2;

p1 = Coor(a1, b1);
p2 = Coor(a2, b2);

if (p1 == p2)
cout<<"Coordinates are equal.";
else
cout<<"Coordinates are not equal.";
return 0;
}
OUTPUT:
20.Create a class Float that contains one float data member. Overload all
the four arithmetic operators so that they can operate on object(s) of Float
(class).

#include "iostream"

using namespace std;

class Float {
float f;

public:
Float(): f(0) {}
Float(float _f): f(_f) {}
Float(Float &F): f(F.f) {}

Float operator * (Float const &F) {


Float temp;
temp.f = float(F.f * f);
return temp;
}
Float operator + (Float const &F) {
Float temp;
temp.f = float(F.f + f);
return temp;
}
Float operator - (Float const &F) {
Float temp;
temp.f = float(F.f - f);
return temp;
}
Float operator / (Float const &F) {
Float temp;
temp.f = float(F.f / f);
return temp;
}

friend ostream & operator << (ostream &out, Float const &F) {
out<<F.f;
return out;
}
~Float() {}
};

int main () {
Float d(5), e(4.3);
cout<<"After overloading \'*\' operator - (d/e) : ";
cout<<d*e<<endl;
cout<<"After overloading \'-\' operator - (d/e) : ";
cout<<d-e<<endl;
cout<<"After overloading \'+\' operator - (d/e) : ";
cout<<d+e<<endl;
cout<<"After overloading \'/\' operator - (d/e) : ";
cout<<d/e<<endl;

return 0;
}
OUTPUT​:

21.Write a program to overload << and >> operators for oject of class Time.

- Write a program overload <=, ==, >= operators to compare the objects of
class Time

- Write a program to perform overloading of function-call operator.

#include "iostream"

using namespace std;

class Time {
int hh, mm, ss;

public:
Time(): hh(0), mm(0), ss(0) {}
Time(int _hh, int _mm, int _ss): hh(_hh), mm(_mm), ss(_ss) {}
Time(Time &t): hh(t.hh), mm(t.mm), ss(t.ss) {}
friend ostream & operator << (ostream &out, Time &t) {
out<<t.hh<<"h "<<t.mm<<"m "<<t.ss<<"s";
return out;
}

friend istream & operator >> (istream &in, Time &t) {


in>>t.hh;
in>>t.mm;
in>>t.ss;
return in;
}

friend bool operator <= (Time &t1, Time &t2) {


if (t1.hh <= t2.hh)
return true;
else
return false;
}
friend bool operator >= (Time &t1, Time &t2) {
if (t1.hh >= t2.hh)
return true;
else
return false;
}
friend bool operator == (Time &t1, Time &t2) {
if (t1.hh == t2.hh && t1.mm == t2.mm && t1.ss == t2.ss)
return true;
else
return false;
}

Time* operator () (int _hh) {


this->hh += _hh;
return this;
}
~Time() {}
};

int main () {
Time T1, T2;

cout<<"Feed time T1: ";


cin>>T1;
cout<<"Time T1 : ";
cout<<T1<<endl;

cout<<"Feed time T2: ";


cin>>T2;
cout<<"Time T2 : ";
cout<<T2<<endl;

cout<<"\nWorking of overloaded >=, <= and == operators: \n";


cout<<"(T1 >= T2): "<<(T1 >= T2)<<endl;
cout<<"(T1 <= T2): "<<(T1 <= T2)<<endl;
cout<<"(T1 == T2): "<<(T1 == T2)<<endl;

cout<<"+++++++++++++++++++++++++++++++++++++++\n";
cout<<"overloaded function call operator or (): \n";
T1(4);
cout<<T1;
return 0;
}
OUTPUT:

22.Write a program to overload subscript operator i.e. []


#include "iostream"

using namespace std;

//global variable
const int maxSIZE = 50;

class safearay {
int arr[maxSIZE];
int size;

public:
safearay(int _i): size(_i) {
register int i;

for(i = 0; i < _i; i++) {


arr[i] = i;
}
}

int & operator [] (int i) {


if( i > this->size ) {
cout << "Index out of bounds" <<endl;
// return first element.
return arr[0];
}

return arr[i];
}
};

int main() {
safearay A(10);

cout << "Value of A[2] : " << A[2] <<endl;


cout << "Value of A[5] : " << A[5]<<endl;
cout << "Value of A[12] : " << A[12]<<endl;

return 0;
}
OUTPUT:
Value of A[2] : 2
Value of A[5] : 5
Value of A[12] : Index out of bounds
0
23.Convert Class Time type data to integer type and vice-versa.
#include "iostream"

using namespace std;

class Time {
int hh, mm, ss;

public:
Time(): hh(0), mm(0), ss(0) {}
Time(int _hh, int _mm, int _ss): hh(_hh), mm(_mm), ss(_ss) {}
Time(Time &t): hh(t.hh), mm(t.mm), ss(t.ss) {}
friend ostream & operator << (ostream &out, Time &t) {
out<<t.hh<<"h "<<t.mm<<"m "<<t.ss<<"s";
return out;
}

friend istream & operator >> (istream &in, Time &t) {


in>>t.hh;
in>>t.mm;
in>>t.ss;
return in;
}

friend bool operator <= (Time &t1, Time &t2) {


if (t1.hh <= t2.hh)
return true;
else
return false;
}
friend bool operator >= (Time &t1, Time &t2) {
if (t1.hh >= t2.hh)
return true;
else
return false;
}
friend bool operator == (Time &t1, Time &t2) {
if (t1.hh == t2.hh && t1.mm == t2.mm && t1.ss == t2.ss)
return true;
else
return false;
}

Time* operator () (int _hh) {


this->hh = _hh;
return this;
}

operator int () {
return int(this->hh);
}
~Time() {}
};

int main () {
Time T1, T2;

cout<<"Feed time T1: ";


cin>>T1;
cout<<"Time T1 : ";
cout<<T1<<endl;

int x = int(T1);

cout<<"\nInt casted t1: "<<x;

T1(5);
cout<<"\nInt to object type: "<<T1;

return 0;
}
OUTPUT​:
Feed time T1: 5
26
9
Time T1 : 5h 26m 9s

Int casted t1: 5


24.Inherit Result class and Fees class from Student to class and implement fee
pay functionality which provides receipt on trans and also implement result
functionality to show the score of the student.
#include "iostream"

using namespace std;


int Groll(1);

class Student {
int rollno;
string name;
double curr;

public:
Student(): curr(0) {}
Student(string _name): name(_name), rollno(Groll), curr(0) { Groll++; }

void pay();
void result();

~Student() {}
};

class Fees: public Student {


double fees;

public:
Fees(): fees(0) {}
// Fees(double _fees): fees(_fees) {}
void reciept(string _name, int num) {
cout<<"+++++++++++++++++Reciept++++++++++++++++\n\n";
cout<<"Name: "<<_name<<endl<<"Roll Number: "<<num<<"\n\nFee paid successfully. Amount:
"<<fees<<endl<<endl;
}
double payTrigger() {
fees += 10000;
return fees;
}

~Fees() {}
};

class Result: public Student {


int per;

public:
Result(): per(80) {}

int score() {
return this->per;
}

~Result() {}
};

void Student::pay() {
Fees f;
this->curr += f.payTrigger();
f.reciept(this->name, this->rollno);
}

void Student::result() {
Result r;
cout<<"You got "<<r.score()<<"% \n";
}
int main () {
Student s("Aditya");

s.pay();
s.result();
return 0;
}
OUTPUT:
+++++++++++++++++Reciept++++++++++++++++

Name: Pranav
Roll Number: 24
Fee paid successfully. Amount: 10000

You got 80%


25.​ college wishes to maintain a Database of it's employees. The DB is divided
into a number of classes whose hierarchical relationship are given below acc.
to this specify all the classes and define methods to create the DB and
retrive individual info. when required:

++++++++
+ Staff +
++++++++
+ code, Name +
++++++++
/ | \
/ | \
/ | \
\/ \|/ \/
++++++++ ++++++++ ++++++++
+ Teacher + + Typist + + Officer +
++++++++ ++++++++ ++++++++
+ Subject, Pub+ + Speed + + Grade +
++++++++ ++++++++ ++++++++
/ \
/ \
\/ \/
++++++++ ++++++++
+ Regular + + Casual +
++++++++ ++++++++
+ + + Daily wages +
++++++++ ++++++++

#include "iostream"

using namespace std;

int Gcode(0);

class Staff {
int code;
string name;
public:
Staff(): name("No_name"), code(0) {code++;}
Staff(string _name) {code++;}
~Staff() {}
};

class Teacher: public Staff {


string sub, pub;

public:
Teacher() {
cout<<"Teacher registered\n";
}
~Teacher() {}
};

class Typist: public Staff {


int speed;

public:
Typist() {
}
void wpm() {
cout<<"Your speed is: "<<this->speed<<" wpm\n";
}
~Typist() {}
};

class Officer: public Staff {


char grade;

public:
Officer(): grade('A') {
cout<<"Officer of grade: A registered\n";
}
Officer(char _grade): grade(_grade) {
cout<<"Officer of grade: "<<this->grade<<" registered\n";
}
~Officer() {}
};

class RTypist: public Typist {


public:
RTypist() {
cout<<"Regular typist registered\n";
}
~RTypist() {}
};
class CTypist: public Typist {
public:
CTypist() {
cout<<"Casual typist registered\n";
}
~CTypist() {}
};
int main () {
RTypist rt1, rt2;
Officer o1, o2('J');
Teacher t1, t2;
return 0;
}
OUTPUT:
Regular typist registered
Regular typist registered
Officer of grade: A registered
Officer of grade: J registered
Teacher registered
Teacher registered
26.Write a program to add two complex numbers using a friend function
#include "iostream"

using namespace std;

class COMPLEX {
int real, img;

public:
COMPLEX(): real(0), img(0) {}
COMPLEX( COMPLEX &c ): real(c.real), img(c.img) {}
COMPLEX(int _real, int _img): real(_real), img(_img) {}

void show () {
char c = '+';

if (img < 0)
c = '-';

cout<<"complex number (a + ib): "<<real<<" "<<c<<" i"<<img<<endl<<endl;


}
friend COMPLEX add( COMPLEX const &c1, COMPLEX const &c2) {
COMPLEX ans(c1.real + c2.real, c1.img + c2.img);
return ans;
}
~COMPLEX() {}
};

int main () {
COMPLEX c1(2, 4), c2(4, 5);
cout<<"C1 : ";
c1.show();
cout<<"C2 : ";
c2.show();

cout<<"Addition of c1 ad c2 using friend function: ";add(c1, c2).show();


return 0;
}
OUTPUT:
C1 : complex number (a + ib): 2 + i4

C2 : complex number (a + ib): 4 + i5

Addition of c1 ad c2 using friend function: complex number (a + ib): 6 + i9


27.Write a program to print the details of students by creating a student
class. If no name is passed while creating object, initialize the object with
"Unknown", otherwise the declared name should be reflected.
#include "iostream"

using namespace std;

//global id;
int id(1);

class Student {
int rno;
string name;

public:
Student(): name("Unknown"), rno(id) {id++;}
Student(string _name): name(_name), rno(id) {id++;}
Student(Student &s): name(s.name), rno(s.rno + 1) {id++;}

void print() {
cout<<"Student\nRoll No. : "<<this->rno<<"\nName: "<<this->name<<endl<<endl;
}
~Student() {}
};

int main () {
Student s1("Rohan"), s2;

s1.print();
s2.print();
return 0;
}

OUTPUT:
Student
Roll No. : 1
Name: Rohan

Student
Roll No. : 2
Name: Unknown

You might also like