OOPs RAW
OOPs RAW
4. Operator Overloading
a. Overload the operator unary(-) for demonstrating operator overloading.
b. Overload the + for concatenating the two strings. For e.g “Py” + “thon” =
Python
5. Inheritance
a. Design a class for single level inheritance using public and private type
derivation.
b. Design a class for multiple inheritance.
7. String handling
a. String operations for string length , string concatenation
8. Exception handling
a. Show the implementation of exception handling
b. Show the implementation for exception handling for strings
9. File handling
a. Design a class FileDemo open a file in read mode and display the total
number of words and lines in the file.
10. Templates
a. Show the implementation of template class library for swap function.
1
1. Classes and Methods
a. Design an employee class for reading and displaying the employee information, the
getInfo() and displayInfo() methods will be used repectively. Where getInfo() will be
private method.
CODE:
#include<iostream>
using namespace std;
class employee
{
char name[20];
int age;
float basic_sal;
void getInfo()
{
cout<<endl<<"Enter name: ";
cin>>name;
cout<<endl<<"Enter age: ";
cin>>age;
cout<<endl<<"Enter basic salary: ";
cin>>basic_sal;
}
public:
void displayInfo()
{
getInfo();
cout<<endl<<"\tEMPLOYEE INFORMATION\n";
cout<<"\t----------- ";
cout<<endl<<" Name: "<<name;
cout<<endl<<" Age: "<<age;
cout<<endl<<" Basic Salary: "<<basic_sal;
cout<<endl<<" Gross Salary: "<<basic_sal + (0.6*basic_sal) + (0.4*basic_sal);
2
}
};
int main()
{
employee e;
e.displayInfo();
}
OUTPUT:
3
b. Write a program to demonstrate function definition outside class and accessing class
members in function definition
CODE:
#include<iostream>
using namespace std;
class Student
{
int roll_no;
char name [30];
float percentage;
public:
void getdata ();
void show ();
};
void Student:: getdata ()
{
cout <<"Enter Roll No: ";
cin >> roll_no;
cout << endl <<"Enter name: ";
cin >> name;
cout << endl <<"Enter Percentage: ";
cin >>percentage;
}
void Student:: show ()
{
cout << endl <<"Roll No: " << roll_no;
cout << endl <<"Name: " << name;
cout << endl <<"Percentage: " <<percentage;
}
int main()
{
Student studObj;
studObj.getdata();
studObj.show();
}
OUTPUT:
4
2. Using friend functions
a. Write a friend function for adding the two complex numbers, using a single class.
CODE:
#include<iostream>
using namespace std;
class complex
{
float n,m;
public:
void getData()
{
cout<<"\nEnter real number: ";
cin>>n;
cout<<"\nEnter imaginary number: ";
cin>>m;
}
void showData()
{
cout<< n <<" + "<< m<<"j" ;
}
friend complex sum(complex, complex);
};
complex sum(complex c1, complex c2)
{
complex c3;
c3.n=c1.n+c2.n;
c3.m=c1.m+c2.m;
return c3; }
5
int main()
{
complex obj1, obj2, obj3;
cout<<"\nEnter Data for 1st Complex Number \n";
cout<<"-------------------------------";
obj1.getData();
cout<<"\nEnter Data for 2nd Complex Number \n";
cout<<" ";
obj2.getData();
obj3=sum(obj1,obj2); cout<<"\nComplex Number1: ";
obj1.showData();
cout<<"\nComplex Number2: ";
obj2.showData();
cout<<"\nComplex Number3: ";
obj3.showData();
}
OUTPUT:
6
3. Constructors and method overloading.
a. Design a class Complex for adding the two complex numbers and also show the use of
constructor.
CODE:
#include<iostream>
using namespace std;
class complex
{
float n,m;
public:
complex()
{
n=0; m=0;
}
complex(int a, int b)
{
n=a;
m=b;
}
void showData()
{
cout<< n <<" + "<< m<<"j" ;
}
complex sum(complex c1)
{
complex c3;
c3.n=n+c1.n;
c3.m=m+c1.m;
return c3;
}
7
};
int main()
{
complex obj1(3,4);
complex obj2(4,5);
complex obj3;
obj3=obj1.sum(obj2);
cout<<"\nComplex Number1: ";
obj1.showData();
cout<<"\nComplex Number2: ";
obj2.showData();
cout<<"\nComplex Number3: ";
obj3.showData();
}
OUTPUT:
8
b. Design a class StaticDemo to show the implementation of static variable and static
function.
CODE:
#include<iostream>
#include<stdio.h>
using namespace std;
class student
{
int roll_no;
char name[30];
float percent;
static int c;
public:
void get()
{
cout<<"Enter Name:";
cin>>name;
cout<<"Enter percentage:";
cin>>percent;
roll_no=++c;
}
void show()
{
cout<<"\nRoll No:"<<roll_no;
cout<<"\nName:"<<name;
cout<<"\nPercentage:"<<percent;
cout<<"\n\tTotal number of students admitted:"<<c;
}
};
9
int student::c; int main()
{
student s1,s2;
s1.get();
s2.get();
cout<<"\n Object 1 Data";
cout<<"\n***************";
s1.show();
cout<<"\n";
cout<<"\nObject 2 Data";
cout<<"\n**************";
s2.show();
}
OUTPUT:
10
4. Operator Overloading
a. Overload the operator unary(-) for demonstrating operator overloading.
CODE:
#include<iostream>
using namespace std;
class abc
{
int a,b,c;
public:
void get()
{
cout<<"\nEnter three numbers: ";
cin>>a>>b>>c;
}
void show()
{
cout<<"\n\nA= "<<a<<"\tB= "<<b<<"\tC= "<<c;
}
11
int main()
{
abc a1;
a1.get();
cout<<"\n\n Original contents";
a1.show();
-a1;
cout<<"\n\n After Negation";
a1.show();
}
OUTPUT:
12
b. Overload the + for concatenating the two strings. For e.g “Py” + “thon” =
Python.
CODE:
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class string
{
char str[60];
public:
void get()
{
cout<<"\n\tEnter a string: ";
cin>>str;
}
void show()
{
cout<<"\n"<<str;
}
string operator +(string s2)
{
string s3;
strcpy(s3.str,str);
strcat(s3.str,s2.str);
return s3;
} };
13
int main()
{
string s1,s2,s3;
s1.get();
s2.get();
cout<<"\nString 1";
s1.show();
cout<<"\nString 2";
s2.show();
cout<<"\nAfter concatenation String 3";
s3=s1 + s2;
s3.show();
}
OUTPUT:
14
5. Inhritance
a. Design a class for single level inheritance using public and private type derivation.
CODE:
(1) Using public type derivation
#include<iostream>
using namespace std;
class base
{
int n;
public:
void get()
{
cout<<"\nEnter value for n:";
cin>>n;
}
void show()
{
cout<<"\n\t\tN="<<n;
}
};
public:
void get()
15
{
base::get();
cout<<"\nEnter value for b: ";
cin>>b;
}
};
int main()
{
derived d1;
d1.get();
d1.show();
}
OUTPUT:
16
(2) Using private type derivation:
#include<iostream>
using namespace std;
class base
{
int n;
public:
void get()
{
cout<<"\nEnter value for n:";
cin>>n;
}
void show()
{
cout<<"\n\t\tN="<<n;
}
};
17
{
base::get();
cout<<"\nEnter value for b: ";
cin>>b;
}
void display()
{
show();
}
};
int main()
{
derived d1;
d1.get();
d1.show();
}
OUTPUT:
18
b. Design a class for multiple inheritance.
CODE:
#include<iostream>
using namespace std;
class internal
{
int n;
public:
void get()
{
cout<<"\nEnter n: ";
cin>>n;
}
int n_return()
{
return n;
}
void show()
{
cout<<"\n\nInternal marks: "<<n;
}
};
class external
{
int m; public:
void get()
19
{
cout<<"\nEnter m: ";
cin>>m;
}
int m_return()
{
return m;
}
void show()
{
cout<<"\nM: "<<m;
}
};
20
};
int main()
{
final t;
t.get();
t.show();
}
OUTPUT:
21
6. Virtual functions and abstract classes
a. Show the use of virtual function
CODE:
#include<iostream>
using namespace std;
class base
{
public:
virtual void display()
{
cout<<"\nDisplay of base class called";
}
};
int main()
{
base *b;
derived d;
22
b=&d;
b->display();
}
OUTPUT:
23
b. Show the implementation of abstract class.
CODE:
#include<iostream>
using namespace std;
class Figure
{
public:
double dim1;
double dim2;
Figure(double a, double b)
{
dim1 = a;
dim2 = b;
}
// pure virtual function
virtual double area()=0;
};
24
return dim1 * dim2;
}
};
OUTPUT:
25
7. String handling
a. String operations for string length , string concatenation.
CODE:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1="Sanjeela"; string str2="Sagar";
cout<<endl<<"Length of "<<str1<<": "<<str1.length();
string str3=str1+str2;
cout<<endl<<str3;
}
OUTPUT:
26
8. Exception handling
a. Show the implementation of exception handling.
CODE:
#include<iostream>
using namespace std;
int main()
{
float percent;
cout<<"Enter your percentage: ";
cin>>percent;
try
{
if(percent<0 || percent>100) throw(percent);
else
cout<<endl<<"Your percentage: "<<percent;
}catch(int p)
{
cout<<endl<<"Invalid percentage: "<<p;
}
}
OUTPUT:
27
b. Show the implementation for exception handling for strings.
CODE:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cout<<"Enter the name of your course: ";
cin>>s;
try
{
if(s=="B.Sc-IT" || s=="BMS" || s=="B.Com")
cout<<endl<<"Your have chosen Course: "<<s;
else throw(s);
} catch(string ss)
{
cout<<endl<<"Oh!!!!!!!!! you have chosen the course that we don't provide: "<<ss;
}
}
OUTPUT:
28
9. File Handling
a. Design a class FileDemo open a file in read mode and display the total number of words
and lines in the file.
CODE:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream fread("WordLineCount.txt");
int wc=1,lc=1;
char c;
while(fread)
{
fread.get(c);
if(c==' '|| c=='\n') wc++;
if(c=='\n')
lc++; }
fread.close();
cout<<"\n Total no. of words in the file: "<<wc;
cout<<"\n Total no. of lines in the file: "<<lc;
}
OUTPUT:
29
10. Templates
a. Show the implementation of template class library for swap function.
CODE:
#include<iostream>
using namespace std;
template<class A>
void swap(A &a,A &b)
{
A t=a;
a=b;
b=t;
};
void main()
{
int n,m;
float f1,f2;
char c,d;
cout<<endl<<"Enter two integers: ";
cin>>n>>m;
cout<<endl<<"Enter two floats: ";
cin>>f1>>f2;
cout<<endl<<"Enter two characters: ";
cin>>c>>d;
30
swap(n,m);
cout<<endl<<"\nIntegers after swapping\n";
cout<<" ";
cout<<endl<<"N= "<<n<<"\tM= "<<m;
OUTPUT:
31