0% found this document useful (0 votes)
28 views24 pages

Oops Practicals

It is a practicals of object-oriented programming using c++ use it as template to create your practical file of object-oriented programming in c++

Uploaded by

sunilvirdi225
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views24 pages

Oops Practicals

It is a practicals of object-oriented programming using c++ use it as template to create your practical file of object-oriented programming in c++

Uploaded by

sunilvirdi225
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DAV Institute of Engineering & Technology

Jalandhar, India

PRACTICAL FILE OF OOPS

Submitted By

DINESH
CSE
10102/24
2301586
Program 1: Write a program that uses a class where the member functions
are defined inside a class.

#include <iostream>
using namespace std;
class student
{
public:
string name;
int roll;
void disp()
{
cout << "Name : " << name << endl;
cout << "Roll Number : " << roll << endl;
}
};

int main()
{
student s1;
[Link] = "Dinesh";
[Link] = 1;
[Link]();
return 0;
}

Output:

1
Program 2: Write a program that uses a class where the member functions
are defined outside a class.

#include <iostream>
using namespace std;
class student
{
public:
string name;
int roll;
void disp();
};
void student::disp()
{
cout << "Name : " << name << endl;
cout << "Roll NUmber : " << roll << endl;
}

int main()
{
student s1;
[Link] = "Dinesh ";
[Link] = 1;
[Link]();
return 0;
}

Output:

2
Program 3: Write a program to demonstrate the use of static data
members.

#include <iostream>
using namespace std;
class student
{
public:
string name;
int roll;
static string branch ;

void disp()
{
cout << "Branch : " << branch << endl;
}
};
string student::branch = "Cse";

int main()
{
student s1,s2;
[Link]();
[Link]();
return 0;
}

Output:

3
Program 4: Write a program to demonstrate the use of const data
members.

#include <iostream>
using namespace std;
class student{
public:
const int roll;
student(int r ) : roll(r){}
};
int main(){
student s1(25);
[Link]=24;
return 0 ;
}

Output:

4
Program 5: Write a program to demonstrate the use of zero argument and
parameterized constructors.

#include <iostream>
using namespace std;
class student{

public:
student(){
cout<<"Zero argument constructor"<<endl;
}
student(string str){
cout<<str<<endl;
}
};
int main(){
student s1;
student s2("Parameterized constructor");
return 0;
}

Output:

5
Program 6: Write a program to demonstrate the use of dynamic
constructor.

#include <iostream>
using namespace std;
class student{

public:
int *roll;
student(){
roll = new int;
*roll = 19;
}
void disp(){
cout<<"Value of roll : "<<*roll<<endl;
}
~student(){
delete roll;
}
};
int main(){
student s1;
[Link]();
return 0;
}

Output:

6
Program 7: Write a program to demonstrate the use of friend function.

#include <iostream>
using namespace std;
class Base{
private:
int x;
protected:
int y;
public:
Base(){
x=10;
y=20;
}
friend void func(Base & obj);
};
void func(Base & obj){
cout<<"private variable : "<<obj.x<<endl;
cout<<"protected variable : "<<obj.y<<endl;
}
int main(){
Base obj1;
func(obj1);
return 0;
}

7
Output:

Program 8: Write a program to demonstrate the use of initializer list.

#include <iostream>
using namespace std;
class Base{
public:
int x,y;
Base(int a,int b) : x(a),y(b){}
int add(){
return x+y;
}
};

int main(){
Base obj1(10,20);
cout<<"Sum of x and y = "<<[Link]();
return 0;
}

Output:

8
Program 9: Write a program to demonstrate the overloading of increment
and decrement operators.

#include <iostream>
using namespace std;
class Base{
public:
int x;
Base(){
x=10;
}
void show(){
cout<<"x = "<<x<<endl;
}
void operator++(){
++x;
}
void operator--(){
--x;
}
};

int main(){

9
Base obj ;
cout<<"before operator overloding"<<endl;
[Link]();
cout<<"After ++ overloading"<<endl;
++obj;
[Link]();
cout<<"After -- overloading"<<endl;
--obj;
[Link]();

return 0;
}

Output:

10
Program 10: Write a program to demonstrate the overloading of memory
management operators.

#include<iostream>
#include<stdlib.h>

using namespace std;


class student
{
string name;
int age;
public:
student()
{
cout<< "Constructor is called\n" ;
}
student(string name, int age)
{
this->name = name;
this->age = age;
}
void display()
{
cout<< "Name:" << name << endl;
cout<< "Age:" << age << endl;
11
}
void * operator new(size_t size)
{
cout<< "Overloading new operator with size: " << size << endl;
void * p = ::operator new(size);

return p;
}
void operator delete(void * p)
{
cout<< "Overloading delete operator " << endl;
free(p);
}
};

int main()
{
student * p = new student("Yash", 24);

p->display();
delete p;
}

Output:

12
Program 11: Write a program to demonstrate the typecasting of basic type
to class type.

#include <iostream>
using namespace std;
class Time {
int hour;
int mins;

public:

Time()
{
hour = 0;
mins = 0;
}

Time(int t)
{
hour = t / 60;
mins = t % 60;
}

void Display()
{
13
cout << "Time = " << hour
<< " hrs and "
<< mins << " mins\n";
}
};

int main()
{
Time T1;
int dur = 150;
T1 = dur;
[Link]();
return 0;
}

Output:

14
Program 12: Write a program to demonstrate the function overloading.

#include <iostream>
using namespace std;
class base{
public:
void add(int a,int b){
cout<<"Sum of a and b : "<<a+b<<endl;
}
void add(int a,int b,int c){
cout<<"sum of a,b,c : "<<a+b+c<<endl;
}
};
int main(){
base b1;
[Link](2,3);
[Link](3,2,4);
return 0;
}

Output:

15
Program 13: Write a program to demonstrate the virtual base function.

#include <iostream>
using namespace std;
class A{
public:
void greeting (){
cout<<"Hello!"<<endl;
}
};
class B: public virtual A{

};
class C: public virtual A{

};
class D:public B,public C{

};
int main(){
D dd;
[Link]();

16
return 0;
}

Output:

Program 14: Write a program to demonstrate the multiple inheritances.

#include <iostream>
using namespace std;
class A{
public:
void fun1 (){
cout<<"function of base class A"<<endl;
}
};
class B{
public:
void fun2 (){
cout<<"function of base class B"<<endl;
}
};
class C: public A, public B{

};
int main(){
C cc;

17
cc.fun1();
cc.fun2();
return 0;
}

Output:

Program 15: Write a program to demonstrate the runtime polymorphism.

#include <iostream>
using namespace std;
class base{
public:
virtual void fun1 (){
cout<<"fun1 of base class"<<endl;
}
void fun2(){
cout<<"fun2 of base class "<<endl;
}

};

class derived : public base{


public:

void fun1 (){


cout<<"fun1 of derived class"<<endl;

18
}
void fun2(){
cout<<"fun2 of derived class"<<endl;
}

};

int main(){
base * ptr;
derived obj;
ptr = &obj;
ptr->fun1();
ptr->fun2();
return 0;
}

Output:

19
Program 16: Write a program to demonstrate the exception handling.

#include <iostream>
#include <stdexcept>
using namespace std;

int main() {
int numerator, denominator;
cout << "Enter numerator: ";
cin >> numerator;
cout << "Enter denominator: ";
cin >> denominator;
try {
if (denominator == 0) {
throw runtime_error("Error: Division by zero is not allowed!");
}
int result = numerator / denominator;
cout << "Result: " << result << endl;

20
}
catch (const runtime_error& e) {
cout << [Link]() << endl;
}
return 0;
}

Output:

Program 17: Write a program to demonstrate the use of class template.

#include <iostream>
using namespace std;
template <class T>
class base{
public :
T value;
base( T v){
value = v;
}
void disp(){
cout<<"value : "<<value<<endl;
}
};
int main() {
base<int> obj1(5);
[Link]();

21
base<double> obj2(5.5);
[Link]();
return 0;
}

Output:

Program 18: Write a program to demonstrate the reading and writing of


mixed type of data.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
int age = 25;
double salary = 55000.50;
string name = "Alice";

ofstream outFile("[Link]");
outFile << "Name: " << name << endl;
outFile << "Age: " << age << endl;

22
outFile << "Salary: " << salary << endl;
[Link]();

string readName;
int readAge;
double readSalary;

ifstream inFile("[Link]");
getline(inFile, readName);
inFile >> readAge;
inFile >> readSalary;
[Link]();

cout << "\nData read from the file:" << endl;


cout << readName << endl;
cout << "Age: " << readAge << endl;
cout << "Salary: " << readSalary << endl;
return 0;
}

Output:

23

You might also like