0% found this document useful (0 votes)
11 views74 pages

Oops Labmanual - 2

This document is a lab manual for Object Oriented Programming (OOP) for students in the Computer Science and Engineering department. It includes an introduction, lab requirements, a list of experiments, advanced programs, project details, and a marking scheme. The manual provides detailed instructions and code examples for various OOP concepts such as inheritance, operator overloading, and exception handling.

Uploaded by

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

Oops Labmanual - 2

This document is a lab manual for Object Oriented Programming (OOP) for students in the Computer Science and Engineering department. It includes an introduction, lab requirements, a list of experiments, advanced programs, project details, and a marking scheme. The manual provides detailed instructions and code examples for various OOP concepts such as inheritance, operator overloading, and exception handling.

Uploaded by

kunal101219
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

OBJECT ORIENTED

PROGRAMMING LAB
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
CIC-257

Faculty name:
Mrs. Priyanka Kushwaha Student name: Hritik
Group: B
Enrollment no: 00627207222

Plot No. 6 B, Knowledge Park – II,


Greater Noida, G. B. Nagar - 201310
INDEX OF THE CONTENTS

1. Introduction to the lab manual 3

2. Lab requirements (details of H/W & S/W to be used) 3

3. List of experiments 4

4. List of Advance programs 6

5. Projects to be allotted 7

6. Format of lab record to be prepared by the students. 8

7. Marking scheme for the practical exam 11

8. Details of the each section of the lab along with the examples,

exercises & expected viva questions. 13


Object Oriented

Programming

PRACTICAL RECORD

PAPER CODE : CIC-257

Name of the student : Hritik

University Roll No. : 00627207222

Branch : CSE

Section/ Group : B
INDEX

Ex Experiment Name Date of Page no Signature of Remarks


p. performanc Faculty
no e
1

7
[Type here]

Wgrite a program to read and display the details of thestudent


using
r classes.
Program on Constructor overloading, Single
Inheritance, Multilevel Inheritance.

Write a program to resolve ambiguity problems in


Multiple Inheritance and friend function.
Program
u to demonstrate the concept of Multilevel
Inheritance.
l
Write a program to resolve ambiguity problem in Multiple
Inheritance
p using scope resolution operator.
l
Write a Program to demonstrate the concept of unary minus
operator overloading using Friend function.
c

Write
i a program to Implement a class string.

Write
n a program to demonstrate the concept of Virtual
Function and Dynamic Binding.

Create
t a class LIST with two pure Virtual Functions
store()
w and retrieve()
Program to raise an exception if an attempt is made torefer
to an element whose index is beyond array size.
t
Program to define a function template for calculatingthe
s re of given numbers with different data type

. 5 | P a g
e
[Type here]

EXPERIMENT -
1
AIM: -
Write a program for multiplication of two matrices using OOPS.
SOFTWARE USED: -
Code blocks (C++).
THEORY: -
Matrix multiplication in C++

We can add, subtract, multiply and divide 2 matrices. To do so, we are taking inputfrom
the user for row number, column number, first matrix elements and second matrix
elements. Then we are performing multiplication on the matrices entered bythe user.

In matrix multiplication first matrix one row element is multiplied by second matrix allcolumn
elements.

CODE: -
#include <iostream>
using namespace std;int
main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
cout<<"enter the number of row=";
cin>>r;
cout<<"enter the number of column=";
cin>>c;
cout<<"enter the first matrix element=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
cout<<"enter the second matrix element=\n";
[Type here]

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>b[i][j];
}
}
cout<<"multiply of the matrix=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<mul[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
[Type here]

OUTPUT: -
[Type here]

EXPERIMENT 2
AIM: -
Write a program to read and display the details of the student using classes, objects and two
member functions get_details and put_details.
SOFTWARE USED: -
Code blocks (C++).
THEORY: -
Classes in C++
Class is an encapsulation of data and coding. Classes are an expanded version of structures.
Structure can only contain multiple variables. Classes can contain multiple variables, even
more, classes can also contain functions as class member.Variables declared in class are called
Data Members(Attributes). Functions declared or defined in class are called Member
Functions.

CODE: -
#include <iostream>
using namespace std;

class student
{
private:
char name[30];int
rollNo;

public:
void getDetails(void);
void putDetails(void);
};

void student::getDetails(void)
{
cout << "Enter name: " ;
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
[Type here]

}
void student::putDetails(void)
{
cout << "Student details:\n";
cout << "Name:"<< name << ",Roll Number:" << rollNo;
}
int main()
{
student s;

s.getDetails();
s.putDetails();

return 0;
}
OUTPUT: -
[Type here]

EXPERIMENT – 3 PROGRAM 1
AIM PROGRAM 1:

Write a program to perform addition of two complex numbers using constructor overloading.
The first constructor which takes no argument is used to create objectswhich are not
initialized, second which takes one argument is used to initialize real and imaginary parts to
equal values and third which takes two arguments is used toinitialize real and imaginary
parts to two different values.

THEORY:
In C++, we can have more than one constructor in a class with same name, as longas each
has a different list of arguments. This concept is known as Constructor Overloading and is
quite similar to function overloading.
• Overloaded constructors essentially have the same name (name of theclass)
and different number of arguments.
• A constructor is called depending upon the number and type of arguments passed.
• While creating the object, arguments must be passed to let compiler know,which
constructor needs to be called.

SOFTWARE USED:
CODE BLOCKS(C++)

CODE:

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

int real, imaginary;


public:
Complex ()

{
real = 0;
imaginary = 0;
}
Complex (int num)
[Type here]

{
real = num;
imaginary = num;
cout << "Complex Number : " << real << " + " << imaginary << "i" << endl;

}
Complex (int r, int i)

{
real = r;
imaginary = i;
cout << "Complex Number : " << r << " + " << i << "i" << endl;

}
Complex (Complex &other)
{
real = other.real;

imaginary = other.imaginary;
cout << "Complex Number : " << real << " + " << imaginary << "i" << endl;
}
Complex sum (Complex &a, Complex &b)
{
Complex result; result.real = a.real + b.real;
result.imaginary = a.imaginary + b.imaginary;

cout << "Result : " << result.real << " + " << result.imaginary << "i";
return result;
}};
int main()

{
int r1, r2, i1, i2;
cout << "Enter 1st Number's Real and Imaginary part\n";cin
>> r1 >> i1;
[Type here]

cout << "Enter 2nd Number's Real and Imaginary part\n";cin


>> r2 >> i2;

Complex num1(r1, i1), num2(r2, i2);


Complex result;

result = result.sum(num1, num2);


return 0;
}

OUTPUT 1:
[Type here]

EXPERIMENT – 3 PROGRAM 2
AIM PROGRAM 2:

Write a program to demonstrate the concept of Single Inheritance

SOFTWARE USED:
CODE BLOCKS(C++)

THEORY:

In C++, inheritance is a process in which one object acquires all the properties and
behaviours of its parent object automatically. In such way, you can reuse, extend or modify
the attributes and behaviours which are defined in other class.

In C++, the class which inherits the members of another class is called derived class and the
class whose members are inherited is called base class. The derived class is the specialized
class for the base class.

CODE:

#include <iostream>
using namespace std;
class multiply
{
public:
int x;

void getdata_x()
{
cout << "Enter the value of x = ";
cin >> x;
}

};
class derive : public multiply
{
private:
[Type here]

int y;
public:

void getdata_y()
{ cout << "Enter the value of y = ";
cin >> y;

} void product()
{ cout << "Product = " << x * y;
} };

int main()
{
derive a;
a.getdata_x();

a.getdata_y();
a.product();
return 0;
}

OUTPUT 2:
[Type here]

EXPERIMENT – 3 PROGRAM 3
AIM PROGRAM 3:

Write a program to demonstrate the concept of Multilevel Inheritance.

SOFTWARE USED:
CODE BLOCKS(C++)

THEORY:
In C++ programming, not only you can derive a class from the base class but you can also
derive a class from the derived class. This form of inheritance is known as multilevel
inheritance.

CODE:

#include <iostream>
using namespace std;

class multiply
{

public:
int x;

void getdata_x()
{

cout << "Enter value of x= "; cin >> x;


}
};
class ans_1 : public multiply
{
public:
int y;

void getdata_y()
{ cout << "\nEnter value of y= "; cin >> y;
}};
[Type here]

class ans_2 : public ans_1


{ private:
int z;
public:
void getdata_z()

{ cout << "\nEnter value of z= "; cin >> z;


} void product()
{ cout << "\nProduct= " << x * y * z;

}};
int main()
{ ans_2 a;
a.getdata_x();
a.getdata_y();

a.getdata_z();
a.product();

return 0;
}

OUTPUT 3:
[Type here]

EXPERIMENT – 4 PROGRAM 1
AIM PROGRAM 1:

Write a program to resolve ambiguity problem in Multiple Inheritance using scoperesolution


operator.

THEORY:
A derived class with two base classes and these two base classes have one common baseclass is
called multipath inheritance.

Ambiguity in C++ occur when a derived class have two base classes and these twobase
classes have one common base class. Consider the following figure:

SOFTWARE USED:
CODE BLOCKS(C++)

CODE:
#include<iostream>
using namespace std;
class Parent1
{ protected:

int num;
public:
Parent1()
{ num = 123456;

}};

class Parent2
{protected:
[Type here]

int num;
public:

Parent2()
{ num = 654321;
} };
class Child: public Parent1, public Parent2
{ public:
void function()

{ cout << "Parent1's num is " << Parent1::num; cout <<


"\tn\tParent2's num is " << Parent2::num;
} };

int main()
{ Child obj;

obj.function();
return 0;
}

OUTPUT
[Type here]

EXPERIMENT – 4 PROGRAM 2
AIM PROGRAM 2:

Write a program to find the greatest of two given numbers in two different classesusing
friend function.

SOFTWARE USED:
CODE BLOCKS(C++)

THEORY:

If a function is defined as a friend function in C++, then the protected and privatedata of
a class can be accessed using the function.

By using the keyword friend compiler knows the given function is a friend function.

For accessing the data, the declaration of a friend function should be done inside thebody of a
class starting with the keyword friend.

CODE:
#include<iostream>
using namespace std;
class numb
{private:
int x, y;
public:

void input()
{cout << "Enter two numbers:";
cin >> x>>y;

}friend void find(numb n);


};void find(numb n)
{if (n.x > n.y)
{cout << "Largest is:" << n.x;
}else
[Type here]

{cout << "Largest is:" << n.y;


}}
int main()
{numb n;
n.input();
find(n);
return 0;
}

OUTPUT
[Type here]

EXPERIMENT – 5 PROGRAM 1
AIM
:
Write a program to demonstrate the concept of unary minus operator
overloading with or without using friend function

SOFTWARE USED: CODE BLOCKS

THEORY:
The operator keyword declares a function specifying what operator-symbol means when
applied to instances of a class. This gives the operator more than one meaning,or "overloads" it.
The compiler distinguishes between the different meanings of an operator by examining the
types of its operands.
The unary operators operate on a single operand and following are the examples of Unary
operators −

• The increment (++) and decrement (--) operators.


• The unary minus (-) operator.
• The logical not (!) operator.
The unary operators operate on the object for which they were called and normally, this
operator appears on the left side of the object, as in !obj, -obj, and ++obj but sometimes they
can be used as postfix as well like obj++ or obj--.

CODE:
#include<iostream>
using namespace std;
class space{
int x;
int y;
int z;
public:
void getdata(int a,int b,int c); void display(void);
friend void operator-(space &s);
};

void space :: getdata(int a,int b,int c){ x=a;y=b;


z=c;
}

void space :: display(void){ cout<<x<<" ";


cout<<y<<" ";
cout<<z<<"\n";
}
[Type here]

void operator-( space &s){


s.x=-s.x;
s.y=-s.y;
s.z=-s.z;
}

int main(){ space s;


s.getdata(10,-20,30); cout<<"S : ";s.display();
-s;
cout<<"S :"; s.display();
return 0;
}

OUTPUT:
[Type here]

EXPERIMENT – 5 PROGRAM 2

AIM:
Implement a class string containing the following functions.
SOFTWARE USED: CODEBLOCKS

THEORY:
String class is created to implement these functions for the string operations.
The program uses the standard C++ library for these functions.The purpose of this programis just to
demonstrate operator overloading to perform these string operations.
Following is the list of the operators overloaded and their effects on the string objects:
1) == : String copy
2) = : String compare
3) + : String concatenation
4) << : Displaying the string from my_string object
5) >> : Reversing the string
6) / : Finding substring

CODE
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#define MAX 50 class
string
{
char str[MAX]; public:

void operator+=(string s1); void


operator==(string s1); void
operator=(string s1);

string()
{ strcpy(str,” “);
}
string(char s[])
{
strcpy(str,s);
}

};

void string::operator+(string s1)


{
if(strlen(str)+strlen(s1.str)<MAX)
[Type here]

{
strcat(str,s1.str); cout<<”\n\nConcatinated
string is: “<<str;
}
else
{
cout<<”\n\nSorry the array size is exceeded.”;
}
}

void string::operator==(string s1)


{
if(strcmp(str,s1.str)==0)
cout<<”\n\nStrings are equal.”; else
cout<<”\n\nStrings are not equal.”;
}

void string::operator=(string s1)


{
strcpy(str,s1.str);
cout<<”\n\nCopy of string is: “<<str;
}

int main()
{
string s1,s2; int
choice;
char ans;

do
{
cout<<”\n1.Concatination of string\n2.Equal\n3.Copy
string\n; cout<<”\n\nEnter your choice: “; cin>>choice;
switch(choice)
{
case 1:
cout<<”\n\nEnter the first string: “; cin>>s1;
cout<<”\n\nEnter the second string: “;
cin>>s2; s1+s2;
break;

case 2:
cout<<”\n\nEnter the first string: “; cin>>s1;
[Type here]

cout<<”\n\nEnter the second string: “;


cin>>s2; s1==s2;
break;

case 3:
cout<<”\n\nEnter the string: “;
cin>>s1; s2=s1;
break;

default:
cout<<”\n\nPlease enter correct choice.”;
break;
}
cout<<”\n\n\nDo you want to continue?(y/n): “; cin>>ans;
}while(ans==’y’ || ans==’Y');

OUTPUT:
[Type here]

EXPERIMENT – 6 PROGRAM 1
AIM
:

Write a program to demonstrate the concept of Virtual Function and Dynamic


Binding.

SOFTWARE USED: CODE BLOCKS

THEORY:
A virtual function is a member function that you expect to be redefined in derived classes.
When you refer to a derived class object using a pointer or a reference to the base class, you
can call a virtual function for that object and execute the derivedclass's version of the
function.

Dynamic binding or late binding is the mechanism a computer program waits until
runtime to bind the name of a method called to an actual subroutine. It is an alternative to
early binding or static binding where this process is performed at compile-time

CODE PART 1:
#include <iostream>
using namespace std;

class Base
{

public:
virtual void print()
{
cout << "Base Function" << endl;
}
};
class Derived : public Base

{
public:
void print()
{
cout << "Derived Function" << endl;
[Type here]

}
};
int main()
{
Derived derived1;
Base* base1 = &derived1;
base1->print();

return 0;
}
OUTPUT: -
[Type here]

CODE PART 2:
#include<iostream>
using namespace std;int

Square(int x)
{

return x * x;
}

int Cube(int x)
{
return x * x * x;
}
main()
{

int x = 2;
int choice;
do
{

cout << "Enter 0 for Square value, 1 for Cube value :\n";cin
>> choice;

}
while (choice < 0 || choice > 1);

int( * ptr)(int);
switch (choice) {
case 0:
ptr = Square;
break;
case 1:
ptr = Cube;
break;
[Type here]

}
cout << "The result is :" << ptr(x);
return 0;
}

OUTPUT: -
[Type here]

EXPERIMENT – 6 PROGRAM 2

AIM:
Create a class LIST with two pure Virtual Functions store() and retrieve().To store avalue
call store() function and to retrieve a value call retrieve() function. Derive two classes stack
and queue from it and override store() and retrieve() function.

SOFTWARE USED: CODEBLOCKS

THEORY:
A virtual function is declared by using the 'virtual' prefix to the declaration of
that function. A call to virtual function is resolved at the run-time by analyzing the actual
type of object that is calling the function. This is known as dynamic binding.

CODE:
#include<iostream>
#include<stdlib.h>
#include<conio.h>
using namespace std;
struct node

{
int data;
node *next;

};
node *head=NULL,*tail=NULL;
class List
{
public:
void view()

{
node *n = head;
if(head==NULL)

{
[Type here]

cout<<"\n No elements found...";


}
else
{

cout<<" ";
while(n!=NULL)
{

if(n->next==NULL)
{
cout<<n->data;
}
else
{
cout<<n->data<<"->";

}
n = n->next;
}
}
}
virtual void store(int n)=0;
virtual int retrive()=0;

};
class Stack :public List
{
public:
void store(int n)
{
node *n1 = new node();

n1->data = n;
[Type here]

n1->next=NULL;
if((head==NULL)&&(tail==NULL))

{
head = n1;
tail = n1;

}
else
{
tail->next = n1;
tail = n1;

}
}
int retrive()
{

if((tail==NULL)&&(head==NULL))
{
return -1;
}
else
{

int n = tail->data;
node *n1 = head;
while((n1->next!=tail)&&(head!=tail))
{

n1 = n1->next;
}
n1->next = NULL;
free(tail);
if(head!=tail)
[Type here]

{
tail = n1;
}
else
{
tail=NULL;
head=NULL;

}
return n;
}
}
};
class Queue:public List
{
public:
void store(int n)

{
node *n1 = new node();

n1->data = n;
n1->next=NULL;
if((head==NULL)&&(tail==NULL))

{
head = n1;
tail = n1;
}
else
{

tail->next = n1;
tail = n1;
[Type here]

}
}
int retrive()
{
if((tail==NULL)&&(head==NULL))

{
return -1;
}

else
{
int n = head->data;
if(head==tail)
{

head = tail = NULL;

}
else
{
head = head->next;
}
return n;
}

}
};
int main()

{
Stack s1;
int ch;
while(1)
{
[Type here]

system("cls");
cout<<"\n\n Menu";
cout<<"\n\n 1. Stack";
cout<<"\n 2. Queue";

cout<<"\n 3. Exit";
cout<<"\n\n Enter your choice - ";
cin>>ch;

if(ch==1)
{

Stack s1;
int ch1;
while(1)
{

system("cls"); cout<<"\n\n
Stack Menu";
cout<<"\n 1. Push Element";
cout<<"\n 2. Pop Element";
cout<<"\n 3. View Stack";
cout<<"\n 4. Exit";
cout<<"\n\n Enter your choice - ";
cin>>ch1;

if(ch1==1)
{
int element;
cout<<"\n Enter the element you want to push - ";
cin>>element;
s1.store(element); cout<<"\n
Element Pushed";
}
[Type here]

else if(ch1==2)
{
int element=0; element
= s1.retrive();
if(element==-1)
{
cout<<"\n Stack is Empty";
}

else
{
cout<<"\n Element Popped = "<<element;
}
}
else if(ch1==3)
{
cout<<"\n Elements in stack from bottom to top:- ";
s1.view();
}

else if(ch1==4)
{
break;
}
else
{

cout<<"\n\n Wrong choice";


}
getch();

}
}
[Type here]

else if(ch==2)
{
Queue q1;
int ch1;
while(1)
{
system("cls");
cout<<"\n\n Queue Menu";

cout<<"\n 1. Push Element";


cout<<"\n 2. Pop Element";

cout<<"\n 3. View Queue";


cout<<"\n 4. Exit";
cout<<"\n\n Enter your choice - ";
cin>>ch1;
if(ch1==1)
{
int element;
cout<<"\n Enter the element you want to push - ";
cin>>element;
q1.store(element); cout<<"\n
Element Pushed";

}
else if(ch1==2)
{
int element=0; element
= q1.retrive();
if(element==-1)

{
cout<<"\n Queue is Empty";
[Type here]

}
else
{
cout<<"\n Element Popped = "<<element;
}

}
else if(ch1==3)
{
cout<<"\n Elements in queue from front to rear:- ";
q1.view();
}
else if(ch1==4)
{
break;
}
else
{

cout<<"\n\n Wrong choice";


}
getch();
}

}
else if(ch==3)
{
exit(0);
}
else

{
cout<<"\n\n Wrong Choice";
[Type here]

}
getch();
}
return 0;
}

OUTPUT:-
[Type here]
[Type here]
[Type here]
[Type here]

EXPERIMENT – 7 PROGRAM 1

AIM:
WAP to raise an exception if an attempt is made to refer to an element whose index isbeyond the
array size.

SOFTWARE USED- Code Blocks

THEORY-

Exception Handling in C++ is a process to handle runtime errors. We perform exception


handling so the normal flow of the application can be maintained even after runtime errors.

In C++, exception is an event or object which is thrown at runtime. All exceptions are derived
from std:: exception class. It is a runtime error which can be handled. If we don'thandle the
exception, it prints exception message and terminates the program.

CODE-

#include<iostream>

using namespace std;int

main()

int arr[]={-1,2};

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

try

if(i>1)

throw i;

else

cout<<"arr["<<i<<"]="<<arr[i]<<endl;

}
[Type here]

catch(int n)

cout<<"Trying to access array index position"<<n;

cout<<"\n Index out of bound exception"<<endl;

return 0;

OUTPUT-
[Type here]

EXPERIMENT – 7 PROGRAM 2

AIM:
WAP to define a function template for calculating the square of given numbers withdifferent
data type

SOFTWARE USED- Code Blocks

THEORY-

Templates are powerful features of C++ which allows you to write generic programs. In simple
terms, you can create a single function or a class to work with different data types using
templates.

A function template works in a similar to a normal function with one key difference. A single
function template can work with different data types at once but, a single normalfunction can
only work with one set of data types.

CODE-

#include <iostream>

using namespace std;

template <class T>

inline T square(T x)

{ T result; result

= x * x;return

result;

};

int main()

{ int i, ii;

float x, xx;

double y, yy;

cout<<"Enter the Value of i:"<<endl;


[Type here]

cin>>i;

cout<<"Enter the Value of x:"<<endl;

cin>>x;

cout<<"Enter the Value of y:"<<endl;

cin>>y;

ii = square<int>(i);

cout <<"Square Of "<< i << "=" << ii << endl;

xx = square<float>(x);

cout<<"Square Of "<< x << "=" << xx << endl;

// Explicit use of template

yy = square<double>(y);

cout<<"Square Of "<< y << "= " << yy << endl;

OUTPUT-
[Type here]

EXPERIMENT – 8 PROGRAM 1

AIM: WAP to demonstrate the use of special functions, constructor and destructor in theclass
template. The program is used to find the bigger of two entered numbers.

SOFTWARE USED: Codeblocks

THEORY:
Templates are powerful features of C++ which allows you to write generic programs. In simple
terms, you can create a single function or a class to work with different data types using
templates.
Templates are often used in larger codebase for the purpose of code reusability andflexibility of
the programs.

Functional Template

A function template works similarly to a normal function, with one key difference.
A single function template can work with different data types at once but a single normalfunction
can only work with one set of data types.
Normally, if you need to perform identical operations on two or more types of data, youuse
function overloading to create two functions with the required function declaration.

CODE:
#include<iostream>
#include<conio.h>
#include<stdio.h>

using namespace std;


template<class T> class
large{
T a,b;
public:
large(T x, T y){
a = x;
b = y;
}
void largest(){
if(a>b){
cout<<"Greater value is: "<<a<<"\n";
}
else{
[Type here]

cout<<"Greater value is: "<<b<<"\n";

}
}
~large(){}
};

int main(){
// clrscr();
large<int>g(23,40);
g.largest();
large<float>h(45.4,89.88);
h.largest();
getch();
return 0;
}

OUTPUT :
[Type here]

EXPERIMENT – 8 PROGRAM 2

AIM: Program to read the class object of student info such as name, age, sex, height and weight
from the keyboard and to store them on a specified file using read() and write() functions. Again
the same file is opened for reading and displaying the contentsof the file on the screen.

SOFTWARE USED: Codeblocks

THEORY:
File handling:
C++ has two basic classes to handle files, ifstream and ofstream. To use them, includethe header
file
fstream. Ifstream handles file input (reading from files), and ofstream handles file output(writing
to
files)

CODE:
//Writing a class object to a file using ofstream class and mode ios::out

#include<iostream>
#include<fstream>

using namespace std;

const int size = 3;

class A
{
private:
char name[40];
int age;
float height;
char gender;
char newline_chr;

public:
void putdata();
void getdata();
[Type here]

};

//Defining the function putdata() to allow user to enter the data member of a object.void A ::
putdata()
{
cout<<"Enter the name : ";
cin.getline(name,40);
cout<<"Enter the age : ";
cin>>age;
cout<<"Enter the height : ";
cin>>height;
cout<<"Enter the gender : ";
cin>>gender;

//This one captures the enter key(newline character) pressed after entering thegender.
cin.get(newline_chr);

void A :: getdata()
{
cout<<"The name is : " << name << "\n";
cout<<"The age is : " << age << "\n";
cout<<"The height is : " << height << "\n";
cout<<"The gender is : " << gender << "\n";
}

int main()
{

//Creating an output stream


ofstream fstream_ob;

//Calling the open function to read and write an object to/from a filefstream_ob.open("File1.txt",
ios::out);
[Type here]

//Creating an array of objects of class AA


ob1[size];

//Calling the putdata() function


for(int i=0;i<size;i++)
{
//Calling putdata() to let user enter the values for data member of an object.ob1[i].putdata();
}

//Calling the write() function to write an array of objects to a file in a binary form.fstream_ob.write( (char
*) & ob1, sizeof(ob1));

cout<<"Congrats! Your array of objects is successfully written to the file \n";

//Creating another empty array of objects of class AA


ob2[size];

//Closing the output stream


fstream_ob.close();

//Creating an intput stream


ifstream ifstream_ob;

//Calling the open function to read and write an object to/from a file
ifstream_ob.open("File1.txt", ios::in);

cout<<"\nReading an array of objects from a file : \n";


[Type here]

//Calling the read() function to read an array of objects from a file and transfer itscontent to an
empty object
ifstream_ob.read( (char *) & ob2, sizeof(ob2));

for(int i=0;i<size;i++)
{
//Calling getdata() to read the values of an object just readob2[i].getdata();
}

//Closing the input stream


ifstream_ob.close();

return 0;
}

OUTPUT :
[Type here]

OOPS CBS-1
Aim: - Write a program having STUDENT as an abstractclass and create derivedclasses
Engineering, Science from it. Create their objects and process them.
Software used: Dev cpp

THEORY:

Abstract Class:
An abstract class is, conceptually, a class that cannot be instantiated and is usuallyimplemented
as a class that has one or more pure virtual (abstract) functions.
A pure virtual function is one which must be overridden by any concrete (i.e., non-abstract)
derived class. This is indicated in the declaration with the syntax " = 0" in the member function's
declaration.

Derived Class:
A class that is created from an existing class. The derived class inherits all members andmember
functions of a base class. The derived class can have more functionality with respect to the Base
class and can easily access the Base class. A Derived class is also called a child class or subclass.

CODE: -

#include<iostream>
#include<conio.h>
using namespace std;
class student
{public:

virtual void getdata()=0; virtual void display()=0;

};
class engineering:public student

{
int r;

char n[10]; public:


void getdata()
[Type here]

{
cout<<"ENGINEERING FACULTY"<<endl;

cout<<"Enter name: ";


cin>>n;

cout<<"Enter roll: ";


cin>>r;
}

void display()
{
cout<<endl<<"Name= "<<n;

cout<<endl<<"Roll= "<<r<<endl<<endl;
}

};
class science:public student
{

int r;
char n[10]; public:
void getdata()

{
cout<<"OOPS FACULTY"; cout<<endl<<"Enter name: ";
cin>>n; cout<<"Enter roll: "; cin>>r;

void display()
{
cout<<endl<<"Name="<<n; cout<<endl<<"Roll="<<r<<endl<<endl;
}

};
int main()
{
[Type here]

student *ptr[3]; engineering e; science s; ptr[0]=&e;


ptr[0]->getdata();

ptr[0]->display(); ptr[2]=&s;
ptr[2]->getdata();
ptr[2]->display();
getch();

OUTPUT: -
[Type here]
[Type here]

CBS – 2 OOPS
AIM – Develop Hospital Management System using all concept of object-oriented
programming.

SOFTWARE USED - Code Blocks

THEORY:
Object-oriented programming – As the name suggests uses objects in programming. Object-
oriented programming aims to implement real-world entities like inheritance, hiding,
polymorphism, etc in programming. The main aim of OOP is to bind together the data and
the functions that operate on them so that no other part of the code canaccess this data except
that function.
Characteristics of an Object-Oriented Programming language

Class: The building block of C++ that leads to Object-Oriented programming is a Class. It is
a user-defined data type, which holds its own data members and memberfunctions, which can
be accessed and used by creating an instance of that class. A class is like a blueprint for an
object.
[Type here]

SOURCE CODE

#include<iostream>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;

struct doctor1
{
int id;
char name[20],Q[20],age[20],exp[20],city[20],special[20];
};
struct patient1
{
int id1;
char name[20],age[20],city[20],dis[20],room[20],sym[20],con[20],date[20],charg[20],bill[20];
};

using namespace
std;class doctor;
class
patient;
class
hospital
{
public:
// int
id=0,id1=0,int
counter=0;
};

class doctor:public hospital


{
public:
int docid=0;
struct doctor1
arr[10];void
add_doc_info()
{
[Type here]

int i,en;
cout<<"How Many Entries you want to
add :";cin>>en;
for(i=1;i<=en;i++)
{
cout<<"Enter Doctor's ID
:"
;cin>>arr[docid].id;
cout<<"Enter Doctor's Name
:";
cin>>arr[docid].name; cout<<"Enter
Doctor's Age :";
cin>>arr[docid].age;
cout<<"Enter Doctor's Qualification:";
cin>>arr[docid].Q;
cout<<"Enter Doctor's Specialization :";
cin>>arr[docid].special;
cout<<"Enter Doctor's Experience :";
cin>>arr[docid].exp;
cout<<"Enter Doctor's city
:"
;cin>>arr[docid].city;
docid++;
counter++;
cout<<"\n";
cout<<"You filled all Entries of "<<i<<" doctor
successfully"<<"\n";cout<<"enter value for "<<" "<<i+1<<"
"<<"doctor"<<"\n";
}
}
void Display()
{
system("cls"
);int n,i;
cout<<"\n Enter the doctor's ID to display
Record :";cin>>n;
if(n==0)
{
cout<<"\n\n OOPS!!!! "<<"\n\n";
cout<<"Note:- No Record To Display Plz Go Back And Enter Some Entries.
............................................................................................................... "<<"\n";
}
else
{
int status=0;
for(i=0;i<docid;i++)
{
[Type here]

if(arr[i].id==n)
{
status=1
;break;
}
}
if(status)
{
cout<<"\n\n";
cout<<"1.Doctor's ID :"<<arr[i].id<<"\n";
cout<<"2.Doctor's Name :"<<arr[i].name<<"\n";
cout<<"3.Doctor's Age :"<<arr[i].age<<"\n";
cout<<"4.Doctor's Qualification :"<<arr[i].Q<<"\n";
cout<<"5.Doctor's Specialization ...........................
:"<<arr[i].special<<"\n";
cout<<"6.Doctor's Experience
:"<<arr[i].exp<<"\n";
cout<<"8.Doctor's city :"<<arr[i].city<<"\n";
cout<<" \n Press Any KEY To choose another Option
";
}
else
{
cout<<" \n\n No such ID in database "<<endl;
cout<<" \n Press Any KEY To choose another Option ";
}
}
getch();
}

void doctor_detail()
{
int i;
if(docid==0
)
{
cout<<" \n\n\n OOPS!!!! "<<"\n\n";
cout<<"Note:- No Record To Display Plz Go Back And Enter Some Entries.
............................................................................................................... "<<"\n";
}
else{
cout<<"****************************"<<"\n"; cout<<"\t \t
\t Details Of All The Doctors In The Hospital \n";
cout<<"****************************"<<"\n
\n";

cout<<"ID"<<"\t \t"<<"specialization"<<"\t \t"<<"Qualification"<<"\t \t"<<"Age";


[Type here]

cout<<"\n \n";

for(i=0;i<docid;i++)
{
cout<<arr[i].id<<"\t \t"<<arr[i].special<<"\t \t \t"<<arr[i].Q<<"\t \t \t"<<arr[i].age<<"\n";
}
cout<<" \n Press Any Button To choose another Option ";
}
getch();
}
void tot_no_of_doc()
{
system("cls"
);int
i=counter;
cout<<"Total Doctor's in Hospital : "<<i<<"\n";
cout<<" \n Press Any Button To choose another Option
"
;getch();
}
};

class patient:public hospital


{
public:
int docid1=0;
struct patient1
arr[10];void
add_pat_info(); void
Display();
void patient_report();
void patient_detail();
void tot_no_of_pat();
void gen_pat_report();
};

void patient :: add_pat_info()


{
int i,en;
cout<<"How Many Entries you want to
add :";cin>>en;
for(i=1;i<=en;i++)
{
cout<<" 1.Enter Patient's ID
:"
;cin>>arr[docid1].id1;
[Type here]

cout<<" 2. Enter patient's Name


:"
;cin>>arr[docid1].name;
cout<<"3. Enter patient's Age
:"
;cin>>arr[docid1].age;
cout<<" 4. Enter patient's Disease
:"
;cin>>arr[docid1].dis;
cout<<" 5. Enter patient's Symptoms
:"
;cin>>arr[docid1].sym;
cout<<" 6. Enter Patient's Room No.
:"
;cin>>arr[docid1].room;
cout<<" 7. Enter Patient's condition Before Admit
:"
;cin>>arr[docid1].con;
cout<<" 8. Enter Patient's ADMIT Date
:"
;cin>>arr[docid1].date;
cout<<" 9. Enter Patient's Room Charge
:"
;cin>>arr[docid1].charg;
cout<<" 10. Enter Patient's Medicine charge
:"
;cin>>arr[docid1].bill;
docid1++;
counter++;
cout<<"\n";
cout<<"You filled all Entries of "<<i<<" patient
successfully"<<"\n";cout<<"enter value for "<<" "<<i+1<<"
"<<"patient"<<"\n";
}
}

void patient :: Display()


{
system("cls"
);int n,i;
cout<<"\n Enter the Patient's ID to display
info :";cin>>n;
if(n==0)
{
cout<<"\n\n OOPS!!!! "<<"\n \n";
cout<<"Note:- No Record To Display Plz Go Back And Enter Some Entries.
............................................................................................................... "<<"\n";
cout<<" \n Press Any KEY To choose another Option ";
[Type here]

}
else
{
int status=0;
for(i=0;i<docid1;i++)
[Type here]

{
if(arr[i].id1==n)
{
status=1
;break;
}
}
if(status==1)
{
cout<<"1.Patient's ID :"<<arr[i].id1<<"\n";
cout<<"2.Patient's Name
:"<<arr[i].name<<"\n";
cout<<"3.Patient's Age :"<<arr[i].age<<"\n";
cout<<"4.Patient's Disease :"<<arr[i].dis<<"\n";
cout<<"5.Patient's Symptoms
:"<<arr[i].sym<<"\n";
cout<<"6.Patient's Room No. :"<<arr[i].room<<"\n";
cout<<"7.Patient's condition Before Admit
:"<<arr[i].con<<"\n";
cout<<"8.Patient's ADMIT Date :"<<arr[i].date<<"\n";
cout<<"9.Patient's Room Charge
:"<<arr[i].charg<<"\n
";cout<<"10.Patient's Medicine charge
:"<<arr[i].bill<<"\n"
;cout<<" \n Press Any KEY To choose another Option ";
}
else{
cout<<" \n\n No such ID in database "<<endl;
cout<<" \n Press Any KEY To choose another Option ";
}
}
getch();
}

void patient :: patient_report()


{
system("cls"
);int i,n;
cout<<"\n Enter the Patient's ID to Display Report :";
cin>>n;
int status=0;
for(i=0;i<docid1;i++)
{
if(arr[i].id1==n)
{
status=1
;break;
[Type here]

}
}
if(status)
{
cout<<"\n\n * Patient's Report * "<<"\n \n";
cout<< "1. Patient's Name "<<arr[i].name<<"\n";
cout<< "2. Patient's Age "<<arr[i].age<<"\n";
cout<< "3. Patient symptoms
"<<arr[i].sym<<"\n";
cout<< "4. Patient Disease "<<arr[i].dis<<"\n";
cout<< "5. Patient Admit Date "<<arr[i].date<<"\n";
cout<< "6. Patient condition At The Time Of Discharge
"<<arr[i].con<<"\n";cout<<"Press Any Key To Go Back. ";
}
else{
cout<<" \n\n No such ID in database "<<endl;
cout<<" \n Press Any KEY To choose another Option ";
}

getch();
}

void patient:: patient_detail()


{
int i;
if(docid1==
0)
{
cout<<" \n\n\n\n\n OOPS!!!! "<<"\n\n\n";
cout<<"Note:- No Record To Display Plz Go Back And Enter Some Entries.
............................................................................................................... "<<"\n";
}
else{ cout<<"****************************"<<"\n";
cout<<"\t \t \t Details Of All The Pateint In The Hospital \n";
cout<<"****************************"<<"\n \n";
cout<<"ID"<<"\t \t"<<"illness"<<"\t \t"<<"ADMITTED Date"<<"\t \t"<<"Age";
cout<<"\n \n";
for(i=0;i<docid1;i++)
{
cout<<arr[i].id1<<"\t \t"<<arr[i].dis<<"\t \t \t"<<arr[i].date<<"\t \t \t"<<arr[i].age<<"\n";
}
cout<<" \n Press Any KEY To choose another Option ";
}
getch();
[Type here]

void patient :: tot_no_of_pat()


{
system("cls"
);int
i=counter;
cout<<"Total Patients in Hospital : "<<i<<"\n"; cout<<"
\n Press Any KEY To choose another Option
........................................................................................ "
;
getch();
}

void patient :: gen_pat_report()


{
system("cls"
);int i,n;
cout<<"\n Enter the Patient's ID to Display Bill
:";cin>>n; int
status=0;
for(i=0;i<docid1;i++)
{
if(arr[i].id1==n)
{
status=1
;break;
}
}
if(status)
{
cout<<"\n\n * Patient's Report * "<<"\n \n";
cout<< "1. Patient's Medicine Charge
"<<arr[i].bill<<"\n";cout<< "2. Patient's Room
Charge
"<<arr[i].charg<<"\n";cout<<"Press Any Key To Go
Back. ";
}

else{
cout<<" \n\n No such ID in database "<<endl;
cout<<" \n Press Any KEY To choose another Option ";
}
getch();

}
[Type here]

int main()
{
//system("color B0");

bool repeat= true;


int
ch1,ch2,ch3,ch4;
doctor d;
patient
p;xyz:
system("cls"
);cout<<"\n";
cout<<"*** Welcome to the Hospital Management System ***
"<<"\n";cout<<" 1. Menu : "<<"\n";;
cout<<" 2. Exit : "<<"\n\n";
cout<<" Enter Your Choice : ";
cin>>ch1;
cout<<"\n\n\n";

if(ch1==1)
{
xyz2:
system("cls")
;
cout<<"\n\n";
cout<<" 1. Enter into Doctor's DataBase
"<<endl;cout<<" 2. Enter into Patient's DataBase
"<<endl;cout<<" 3. Generate Patient's Report
"<<endl;
cout<<" 4. Generate Patient's Bill "<<endl;
cout<<" 5. EXIT "<<"\n\n";
cout<<" Please Enter Your choice :"<<" ";
cin>>ch2;
while(repeat==true)
{
system("cls"
);
switch(ch2)
case 1:
{
cout<<"\n";
cout<<" * Welcome To Doctor's DataBase * "<<"\n\n";

cout<< " \t 1. Add New Doctor's Information "<<endl


;
cout<< " \t 2. Display Doctor's Information "<<endl;
[Type here]

cout<< " \t 3. Detail OF ALL The Doctors In The Hospital


"<<endl;cout<< " \t 4. Total Number of
Doctor's in Hospital "<<endl; cout<< " \t5.
EXIT "<<"\n\n";

cout<<" Please Enter your choice :


"<<" ";cin>>ch3;
switch(ch3)
{
case 1:
system("cls");
d.add_doc_info(
);break;
case 2:
d.Display()
;
cout<<"\n";
break;

case 3:
system("cls");
d.doctor_detail(
);break;

case 4:
d.tot_no_of_doc();
break;

case 5:
goto
xyz2;
break;

default:
cout<<"invalid"
;
}
break
;case 2:
cout<<"\n\n";
cout<<" * Welcome To Patient's DataBase * "<<"\n\n";

cout<< "1. Add New Patient's Information "<<endl;


cout<< "2. Display Patient's Information "<<endl;
cout<< "3. Detail OF ALL The Patient In The Hospital
"<<endl;cout<< "4. Total
Number of Patient's in Hospital "<<endl;
[Type here]

cout<< "5. EXIT "<<"\n\n";

cout<<" Please Enter your choice :


"<<" ";cin>>ch4;
switch(ch4)
{
case 1:
system("cls");
p.add_pat_info(
);break;
case 2:
p.Display()
;
cout<<"\n";
break;

case 3:
system("cls");
p.patient_detail()
;break;

case 4:
p.tot_no_of_pat();
break;

case 5:
goto
xyz2;
break;

default:
cout<<"invalid"
;break;
}
break;
case 3:

p.patient_report();
goto xyz2;
break;
case 4:
p.gen_pat_report(
);goto xyz2;
break;
[Type here]

case 5:
goto
xyz;
break;

}
}
}
else if(ch1==2)
{
return 0;
}
else
{
cout<<"Wrong Input"<<endl;
}
return 0;
}
[Type here]

OUTPUT
[Type here]
[Type here]

You might also like