0% found this document useful (0 votes)
49 views

Files 3 2021 September NotesHubDocument 1632938285

This document is a lab assessment sheet for an Object Oriented Programming lab course. It lists 37 experiments the student Nikhil Mathur completed on various dates throughout the semester. The experiments cover topics like determining if a number is prime, matrix multiplication, function overloading, classes, inheritance, templates, and file input/output. The sheet tracks the student's work, provides space for instructor comments, and includes signatures to verify completion.

Uploaded by

ad599066
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Files 3 2021 September NotesHubDocument 1632938285

This document is a lab assessment sheet for an Object Oriented Programming lab course. It lists 37 experiments the student Nikhil Mathur completed on various dates throughout the semester. The experiments cover topics like determining if a number is prime, matrix multiplication, function overloading, classes, inheritance, templates, and file input/output. The sheet tracks the student's work, provides space for instructor comments, and includes signatures to verify completion.

Uploaded by

ad599066
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 103

OOPS USING C++ (ETCS-258)

OBJECT ORIENTED PROGRAMMING LAB


ETCS-258

Faculty name: Ms. ZAMEER FATIMA


Student name: NIKHIL MATHUR
Roll No.: 05214802719
Semester: 4th

Maharaja Agrasen Institute of Technology, PSP Area,


Sector – 22, Rohini, New Delhi – 110085.

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)
Object Oriented Programming Lab
(ETCS -258)
Lab Assessment Sheet.

Student En. No.: 05214802719 Student Name: NIKHIL MATHUR

Lab Experiment Total Signature


Date Marks
No. marks
R1 R2 R3 R4 R5
1. Write a program to find whether
a number is prime or not using
OOP.
15/03/21
1 2. Write a program for
multiplication of two matrices
using OOP.
3. Write a program to take name,
address as character string, age
as int, salary as float and
contains inline function to set
the values and display it.
4. Using, the concept of function
overloading, write function for
calculating area of triangle, circle
and rectangle.
2 5. Create a class Student, which
22/03/21
have data members as name,
branch, rollno, age, sex, five
subjects. Display the name of
student & his percentage who
has more than 70%.
6. Write a program to generate a
magic square using OOP.
7. Create a class Time with
members hours, minutes,
seconds. Take input, add two
objects passing objects to
function and display result.
8. Write a program to enter any
number and find its factorial
using constructor.

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

3
9. Write a program to perform 26/04/21
addition of two complex
numbers using constructor
overloading. The first
constructor which takes no
argument is used to create
objects which are not initialized,
second which takes one
argument is used to initialize real
and imag parts to equal values
and third which takes two
argument is used to initialize real
and imag to two different values.
10. Write a program to generate a
Fibonacci series using copy
constructor.
11. Create a class which keep track
of number of its instances. Use
static data member,
constructors and destructors to
4 maintain updated information
about active objects.
12. Write a program to access 03/05/21
members of a student class
using pointer to object members
(or using indirection operator).
13. Write a program to demonstrate
the use of “this” pointer.
14. Write a program to find the
biggest of three numbers using
friend function.
15. Write a program to demonstrate
5 the use of friend function with
inline assignment.
16. Write a program to find the
greater of two given numbers in 17/05/21
two different classes using friend
function.
17. Write a program to find
the sum of two numbers
declared in a class and display
the numbers and sum using
friend class.
18. Write a program to
overload unary increment (++)
operator.
24/05/21
6 19. Write a program to
overload binary + operator.
20. Write a program to
overload less than (<) operator.

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)
21. Write a program to
overload assignment (=)
operator.
22. Write a program to
overload new and delete
operator.
23. Write a program to
overload unary minus(-)
7 operator using friend function.
24. Create a base class
basic_info with data members
name ,roll no, sex and two 31/05/21
member functions getdata and
display. Derive a class
physical_fit from basic_info
which has data members height
and weight and member
functions getdata and display.
Display all the information using
object of derived class.
25. Create class first with data
members book no, book name
and member function getdata()
and putdata(). Create a class
second with data members
author name, publisher and
members getdata() and
showdata(). Derive a class third
from first and second with data
member no of pages and year of
publication. Display all these
information using array of
8 objects of the third class.
26. Design three classes
STUDENT , EXAM and RESULT.
The STUDENT class has data
members such as rollno, name.
07/06/21
create a class EXAM by inheriting
the STUDENT class. The EXAM
class adds data members
representing the marks scored in
six subjects. Derive the RESULT
from the EXAM class and has its
own data members such as total
marks. Write a program to
model this relationship.

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)
27. Create a base class called
SHAPE. Use this class to store
two double type values. Derive
two specific classes called
TRIANGLE and RECTANGLE from
the base class. Add to the base
class, a member function get
data to initialize base class data
members and another member
function display to compute and
display the area of figures. Make
display a virtual function and
redefine this function in the
derived classes to suit their
requirements. Using these three
classes design a program that
will accept driven of a TRINGLE
or RECTANGLE interactively and
display the area.
28. Create a class called LIST
with two pure virtual function
store() and retrieve(). To store a
value call store and to retrieve
call retrieve function, Derive two
classes stack and queue from it
and override store and retrieve.
29. Write a program to
illustrate how to define and
declare a class template for
reading two data items from the
keyboard and to find their sum.

30. Write a program to


illustrate how template
functions can be Overloaded.
9
31. Write a program to define the 14/06/21
function template for calculating
the square of given numbers
with different data types.

32. Write a program to define a


function template for swapping
two items of the various data
types such as integer, float and
character.

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)
33. Write a program to demonstrate
the use of special functions,
constructor and destructor in
the class template. The program
is used to find the biggest of two
entered numbers.
34. Write a program to read a set of
lines from keyboard and store it
on a specified file.
35. Write a program to read a text
10 file and display its contents on
the Screen.
21/06/21
36. Write a program to copy the
contents of a file into another.

37. Write a program to read the


class object of student_info such
as name, age, sex, height and
weight from the keyboard and to
store them in a file using read()
and write() functions. Again the
same file is opened for reading
and displaying the contents of
the file on the screen.

Overall Comments:

Faculty Name: Ms. Zameer Fatima

Signature

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

LAB-1

15-03-21

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 1.
AIM : Write a programme to find whether a number is prime or not .

THEORY :

A number which is only divisible by itself and 1 is known as a prime number, for
example: 5 is a prime number because it is only divisible by itself and 1.
This program takes the value of number (entered by user) and checks whether
the number is prime number or not.

ALGORITHM :

START

Step 1 → Take integer variable A

Step 2 → Divide the variable A with (A-1 to 2)

Step 3 → If A is divisible by any value (A-1 to 2) it is not prime

Step 4 → Else it is prime

STOP

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)
CODE :

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 2.

AIM : Write a program for multiplication of matrices using OOPS.

THEORY :

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

In matrix multiplication, the first matrix one row element is multiplied by the
second matrix of all column elements.

Let's try to understand the matrix multiplication of 3*3 and 3*3 matrices by the
figure given below:

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 3.

AIM : Write a program to take name, address as character string, age as int,
salary as float and contains an inline function to set the values and display
it.

THEORY:

String is a collection of characters. There are two types of strings commonly


used in C++ programming language:
1. Strings that are objects of string class (The Standard C++ Library string class)
2. C-strings (C-style Strings)

C-strings :
In C programming, the collection of characters is stored in the form of
arrays. This is also supported in C++ programming. Hence it's called C-strings.
C-strings are arrays of type char terminated with null character, that is, \0 (ASCII
value of null character is 0).

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

LAB-2

22-03-21

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 4.

AIM : Write a program to find the area of triangle, circle and rectangle using
the concept of function overloading.

THEORY:

Function Overloading in C++ :


Function overloading is a feature in C++ where two or more functions can have
the same name but different parameters.
When a function name is overloaded with different jobs it is called Function
Overloading.
In Function Overloading “Function” name should be the same and the arguments
should be different.
Function overloading can be considered as an example of polymorphism feature
in C++.

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 5.
AIM : Create a class Student, which have data members as name, branch,
rollno, age, sex, five subjects. Display the name of the student & his
percentage who has more than 70%.

THEORY:

C++ Class Definitions :


When you define a class, you define a blueprint for a data type. This doesn't
actually define any data, but it does define what the class name means, that is,
what an object of the class will consist of and what operations can be performed
on such an object.
A class definition starts with the keyword class followed by the class name; and
the class body, enclosed by a pair of curly braces. A class definition must be
followed either by a semicolon or a list of declarations. For example, we defined
the Box data type using the keyword class as follows −
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};

The keyword public determines the access attributes of the members of the class
that follows it. A public member can be accessed from outside the class
anywhere within the scope of the class object. You can also specify the members
of a class as private or protected which we will discuss in a sub-section.

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 6.

AIM : Write a program to create a class TIME with members hours, minutes
and seconds. Take input, add two objects by passing them to function and
display the result.

THEORY :

Define C++ Objects :


A class provides the blueprints for objects, so basically an object is created from
a class. We declare objects of a class with exactly the same sort of declaration
that we declare variables of basic types. Following statements declare two
objects of class Box −
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
Both of the objects Box1 and Box2 will have their own copy of data members.

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

LAB-3

26-04-21

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 7.

AIM : Write a program to access members of a student class using pointer


to object members(or using indirection operation).

THEORY :

C++ Class Definitions :


When you define a class, you define a blueprint for a data type. This doesn't
actually define any data, but it does define what the class name means, that is,
what an object of the class will consist of and what operations can be performed
on such an object. A class definition starts with the keyword class followed by
the class name; and the class body, enclosed by a pair of curly braces. A class
definition must be followed either by a semicolon or a list of declarations. For
example, we defined the Box data type using the keyword class as follows −
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};

The keyword public determines the access attributes of the members of the class
that follows it. A public member can be accessed from outside the class
anywhere within the scope of the class object. You can also specify the members
of a class as private or protected which we will discuss in a sub-section.

Define C++ Objects :


A class provides the blueprints for objects, so basically an object is created from
a class. We declare objects of a class with exactly the same sort of declaration
that we declare variables of basic types. Following statements declare two
objects of class Box −
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
Both of the objects Box1 and Box2 will have their own copy of data members.

CODE :
NIKHIL MATHUR 05214802719
OOPS USING C++ (ETCS-258)

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 8.

AIM : Write a program to generate magic square using OOPS.

THEORY :

Magic Square :
The magic square is a square matrix, whose order is odd and where the sum of
the elements for each row or each column or each diagonal is same.

The sum of each row or each column or each diagonal can be found using this
formula. n(n2+ 1)/2
Here are the rules to construct a magic square −

 We will start from the middle column of the first row, of the matrix, and
always go to the top left corner to place next number
 If the row exceeds, or the row is not in the matrix, then, change the column
as left column and place the number at last row of the matrix, and go for top
left corner again.
 If the column exceeds, or the column is not in the matrix, then change the
row as top row and place the number at last column of that matrix, then go
to the top left corner again.
 When the top left corner is not vacant or both row and column exceeds the
range, then place the number at the bottom of the last-placed number.

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 9.

AIM : Write a program to enter any number and find its factorial using the
constructor.

THEORY :

C++ Constructor :

In C++, constructor is a special method which is invoked automatically at the


time of object creation. It is used to initialize the data members of new object
generally. The constructor in C++ has the same name as class or structure.

There can be two types of constructors in C++.

o Default constructor - A constructor which has no argument is known as


default constructor. It is invoked at the time of creating object.

o Parameterized constructor - A constructor which has parameters is called


parameterized constructor. It is used to provide different values to distinct
objects.

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

CODE :

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 10.

AIM : Write a program to perform addition of two complex numbers using


constructor overloading. The first constructor which takes no argument is
used to create objects which are not initialized, second which takes one
argument is used to initialize real and imaginary parts to equal value and
third which takes two arguments is used to initialize real and imaginary
parts to two different values.

THEORY :

Constructor Overloading in C++ :

In C++, We can have more than one constructor in a class with same name, as
long as 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 the
class) 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.

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

LAB-4

03-05-21

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 11.

AIM : Write a program to generate a Fibonacci series using copy


constructor.

THEORY :

Copy Constructor in C++ :

A copy constructor is a member function that initializes an object using another


object of the same class. A copy constructor has the following general function
prototype:

ClassName (const ClassName &old_obj);

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 12.

AIM : Create a class which keeps track of the number of its instances. Use
static data members,constructors and deconstructors to maintain updated
information about active objects.

THEORY :

Static data members in C++ :

Static data member are class members that are declared using static keyword A
static member has certain special characteristics These are:
 Only one copy of that member is created for the entire class and is shared
by all the objects of that class , no matter how many objects are created.
 It is initialized to zero when the first object of its class is created .No other
initialization is permitted
 It is visible only within the class,but its lifetime is the entire program

Syntax : static data_type data_member_name;

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 13.

AIM : Write a program to demonstrate use of "this" pointer.

THEORY :

‘This’ pointer :
Every object in C++ has access to its own address through an important pointer
called this pointer. The this pointer is an implicit parameter to all member
functions. Therefore, inside a member function, this may be used to refer to the
invoking object.
Friend functions do not have a this pointer, because friends are not members of
a class. Only member functions have a this pointer.

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

LAB-5

17-05-21

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 14.

AIM : Write a program to find the biggest of three numbers using the friend
function.

THEORY :

Friend Function Like friend class, a friend function can be given a special grant
to access private and protected members. A friend function can be:
a) A member of another class
b) A global function

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 15.

AIM : Write a program to demonstrate the use of a friend function with


inline assignment.

THEORY :

Inline Friend Function :

The friend functions, may also be declared as inline functions. If any friend
function is declared inside the class or within the scope of the class definition,
then the inline code substitution is automatically assigned to it. But if the friend
function id defined outside the scope of class definition, then it is required to
assign a keyword inline before the return type. The following example shows the
assignment of inline to a friend function.

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

CODE :

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 16.

AIM : Write a program to find the greatest of two given numbers in two
different classes using a friend function.

THEORY :

Friend Function Like friend class, a friend function can be given a special grant
to access private and protected members. A friend function can be:
a) A member of another class
b) A global function

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 17.

AIM : Write a program to find the sum of two numbers declared in a class
and display the numbers and sum using the friend function.

THEORY :

Friend Function Like friend class, a friend function can be given a special grant
to access private and protected members. A friend function can be:
a) A member of another class
b) A global function

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

LAB-6

24-05-21

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 18.

AIM : Write a program to overload unary increment(++).

THEORY :

Overloading the increment operator :

The operator symbol for both prefix(++i) and postfix(i++) are the same. Hence,
we need two different function definitions to distinguish between them. This is
achieved by passing a dummy int parameter in the postfix version.

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 19.

AIM : Write a program to overload binary + operator.

THEORY :

A binary operator is an operator that operates on two operands and manipulates


them to return a result. Operators are represented by special characters or by
keywords and provide an easy way to compare numerical values or character
strings.
Binary operators are presented in the form:
Operand1 Operator Operand2

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 20.

AIM : Write a program to overload less than(<) operator.

THEORY :

A binary operator is an operator that operates on two operands and manipulates


them to return a result. Operators are represented by special characters or by
keywords and provide an easy way to compare numerical values or character
strings.
Binary operators are presented in the form:
Operand1 Operator Operand2

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 21.

AIM : Write a program to overload assignment(=) operator.

THEORY :

You can overload the assignment operator (=) just as you can other operators
and it can be used to create an object just like the copy constructor.

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

LAB-7

31-05-21

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 22.

AIM : Write a program to overload new and delete operator.

THEORY :

Overloading New and Delete operator in C++ :


The new and delete operators can also be overloaded like other operators in
C++. New and Delete operators can be overloaded globally or they can be
overloaded for specific classes.
If these operators are overloaded using member function for a class, it means
that these operators are overloaded only for that specific class.
If overloading is done outside a class (i.e. it is not a member function of a class),
the overloaded ‘new’ and ‘delete’ will be called anytime you make use of these
operators (within classes or outside classes). This is global overloading.

Syntax for overloading the new operator :


void* operator new(size_t size);

The overloaded new operator receives size of type size_t, which specifies the
number of bytes of memory to be allocated. The return type of the overloaded
new must be void*.The overloaded function returns a pointer to the beginning of
the block of memory allocated.

Syntax for overloading the delete operator :


void operator delete(void*);

The function receives a parameter of type void* which has to be deleted.


Function should not return anything.
NOTE: Both overloaded new and delete operator functions are static
members by default. Therefore, they don’t have access to this pointer .

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 23.

AIM : Write a program to overload unary minus(-) operator using friend


function.

THEORY :

Unary operator acts on one operand only. In case overloaded operator function
is a class member function, then it will act on the object with which it is called and
use it as operand. Hence we need not to pass any extra argument in unary
operator function if its class member function.

Let’s see how to overload Unary Minus ( – ) operator for above class i.e.

/*
* Overloaded unary minus operator as member function.
* It returns a new Object.
*/
ComplexNumber ComplexNumber::operator-() const
{
return ComplexNumber(-(this->real), -(this->imaginary) );
}

It returns a new object with modified values.

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

CODE :

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 24.

AIM : Create a base class basic_info with data members name ,roll no, sex
and two member functions getdata and display. Derive a class physical_fit
from basic_info which has data members height and weight and member
functions getdata and display. Display all the information using the object
of the derived class.

THEORY :

A member function of a class is a function that has its definition or its prototype
within the class definition like any other variable. It operates on any object of the
class of which it is a member, and has access to all the members of a class for
that object.
Let us take previously defined class to access the members of the class using a
member function instead of directly accessing them −

class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
double getVolume(void);// Returns box volume
};

Member functions can be defined within the class definition or separately


using scope resolution operator, : −. Defining a member function within the class
definition declares the function inline, even if you do not use the inline specifier.

So either you can define Volume() function as below −

class Box
{
public:
double length; // Length of a box
NIKHIL MATHUR 05214802719
OOPS USING C++ (ETCS-258)
double breadth; // Breadth of a box
double height; // Height of a box

double getVolume(void)
{
return length * breadth * height;
}
};

If you like, you can define the same function outside the class using the scope
resolution operator (::) as follows −

double Box::getVolume(void)
{
return length * breadth * height;
}

Here, only important point is that you would have to use class name just before ::
operator. A member function will be called using a dot operator (.) on a object
where it will manipulate data related to that object only as follows −

Box myBox; // Create an object

myBox.getVolume(); // Call member function for the object

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

LAB-8

07-06-21

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 25.

AIM : Create class first with data members book no, book name and
member function getdata() and putdata(). Create a class second with data
members author name, publisher and members getdata() and showdata().
Derive a class third from first and second with data member no of pages
and year of publication. Display all this information using an array of
objects of the third class.

THEORY :

A member function of a class is a function that has its definition or its prototype
within the class definition like any other variable. It operates on any object of the
class of which it is a member, and has access to all the members of a class for
that object.
Let us take previously defined class to access the members of the class using a
member function instead of directly accessing them −

class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
double getVolume(void);// Returns box volume
};

Member functions can be defined within the class definition or separately


using scope resolution operator, : −. Defining a member function within the class
definition declares the function inline, even if you do not use the inline specifier.

So either you can define Volume() function as below −

class Box
{
public:
NIKHIL MATHUR 05214802719
OOPS USING C++ (ETCS-258)
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box

double getVolume(void)
{
return length * breadth * height;
}
};

If you like, you can define the same function outside the class using the scope
resolution operator (::) as follows −

double Box::getVolume(void)
{
return length * breadth * height;
}

Here, only important point is that you would have to use class name just before ::
operator. A member function will be called using a dot operator (.) on a object
where it will manipulate data related to that object only as follows −

Box myBox; // Create an object

myBox.getVolume(); // Call member function for the object

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 26.

AIM : Design three classes STUDENT , EXAM and RESULT. The STUDENT
class has data members such as rollno, name. create a class EXAM by
inheriting the STUDENT class. The EXAM class adds data members
representing the marks scored in six subjects. Derive the RESULT from the
EXAM class and has its own data members such as total marks. Write a
program to model this relationship.

THEORY :

C++ Classes :

Class: A class in C++ is the building block, that leads to Object-Oriented


programming. It is a user-defined data type, which holds its own data members
and member functions, which can be accessed and used by creating an instance
of that class. A C++ class is like a blueprint for an object.

For Example: Consider the Class of Cars. There may be many cars with different
names and brand but all of them will share some common properties like all of
them will have 4 wheels, Speed Limit, Mileage range etc.
So here, Car is the class and wheels, speed limits, mileage are their properties.
1. A Class is a user defined data-type which has data members and member
functions.
2. Data members are the data variables and member functions are the
functions used to manipulate these variables and together these data
members and member functions defines the properties and behavior of the
objects in a Class.
3. In the above example of class Car, the data member will be speed
limit, mileage etc and member functions can be apply brakes, increase
speed etc.

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)
CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 27.

AIM : Create a base class called SHAPE. Use this class to store two double
type values. Derive two specific classes called TRIANGLE and
RECTANGLE from the base class. Add to the base class, a member
function getdata to initialize base class datamembers and another member
function display to compute and display the area of figures. Make display a
virtual function and redefine this function in the derived classes to suit
their requirements. Using these three classes design a program that will
accept driven of a TRINGLE or RECTANGLE interactively and display the
area.

THEORY :

C++ Classes :

Class: A class in C++ is the building block, that leads to Object-Oriented


programming. It is a user-defined data type, which holds its own data members
and member functions, which can be accessed and used by creating an instance
of that class. A C++ class is like a blueprint for an object.

For Example: Consider the Class of Cars. There may be many cars with different
names and brand but all of them will share some common properties like all of
them will have 4 wheels, Speed Limit, Mileage range etc.
So here, Car is the class and wheels, speed limits, mileage are their properties.
4. A Class is a user defined data-type which has data members and member
functions.
5. Data members are the data variables and member functions are the
functions used to manipulate these variables and together these data
members and member functions defines the properties and behavior of the
objects in a Class.
In the above example of class Car, the data member will be speed
limit, mileage etc and member functions can be apply brakes, increase speed etc.

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 28.

AIM : Create a class called LIST with two pure virtual functions store() and
retrieve(). To store a value call store and to retrieve call retrieve function,
Derive two classes stack and queue from it and override store and retrieve.

THEORY :

C++ Classes :

Class: A class in C++ is the building block, that leads to Object-Oriented


programming. It is a user-defined data type, which holds its own data members
and member functions, which can be accessed and used by creating an instance
of that class. A C++ class is like a blueprint for an object.

For Example: Consider the Class of Cars. There may be many cars with different
names and brand but all of them will share some common properties like all of
them will have 4 wheels, Speed Limit, Mileage range etc.
So here, Car is the class and wheels, speed limits, mileage are their properties.
6. A Class is a user defined data-type which has data members and member
functions.
7. Data members are the data variables and member functions are the
functions used to manipulate these variables and together these data
members and member functions defines the properties and behavior of the
objects in a Class.
In the above example of class Car, the data member will be speed
limit, mileage etc and member functions can be apply brakes, increase speed etc.

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

LAB-9

14-06-21

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 29.

AIM : Write a program to illustrate how to define and declare a class


template for reading two data items from the keyboard and to find their
sum.

THEORY : Class Templates Like function templates, class templates are useful
when a class defines something that is independent of the data type. Can be
useful for classes like Linked List, Binary Tree, Stack, Queue, Array, etc.

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 30.

AIM : Write a program to illustrate how template functions can be


Overloaded.

THEORY :

Templates in C++

A template is a simple and yet very powerful tool in C++. The simple idea is to
pass data type as a parameter so that we don’t need to write the same code for
different data types. For example, a software company may need sort() for
different data types. Rather than writing and maintaining the multiple codes, we
can write one sort() and pass data type as a parameter.
C++ adds two new keywords to support templates: ‘template’ and ‘typename’.
The second keyword can always be replaced by keyword ‘class’.

How do templates work?


Templates are expanded at compiler time. This is like macros. The difference is,
the compiler does type checking before template expansion. The idea is simple,
source code contains only function/class, but compiled code may contain
multiple copies of same function/class.

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

CODE :

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 31.

AIM : Write a program to define the function template for calculating the
square of given numbers with different data types.

THEORY : Function Templates We write a generic function that can be used for
different data types. Examples of function templates are sort(), max(), min(),
printArray().

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 32.

AIM : Write a program to define a function template for swapping two


items of the various data types such as integer, float and character.

THEORY : Function Templates We write a generic function that can be used for
different data types. Examples of function templates are sort(), max(), min(),
printArray().

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

LAB-10

21-06-21

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 33.

AIM : Write a program to demonstrate the use of special functions,


constructor and destructor in the class template. The program is used to
find the biggest of two entered numbers.

THEORY :

The Class Constructor.


A class constructor is a special member function of a class that is executed
whenever we create new objects of that class.
A constructor will have exact same name as the class and it does not have any
return type at all, not even void. Constructors can be very useful for setting initial
values for certain member variables.

Parameterized Constructor.
A default constructor does not have any parameter, but if you need, a constructor
can have parameters. This helps you to assign initial value to an object at the
time of its creation.

The Class Destructor.


A destructor is a special member function of a class that is executed whenever
an object of it's class goes out of scope or whenever the delete expression is
applied to a pointer to the object of that class.
A destructor will have exact same name as the class prefixed with a tilde (~) and
it can neither return a value nor can it take any parameters. Destructor can be
very useful for releasing resources before coming out of the program like closing
files, releasing memories etc.

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

CODE :

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 34.

AIM : Write a program to read a set of lines from keyboard and store it
on a specified file.

CODE :

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 35.

AIM : Write a program to read a text file and display its contents on the
Screen.

CODE :

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 36.

AIM :Write a program to copy the contents of a file into another.

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

EXPERIMENT 37.

AIM :Write a program to read the class object of student_info such as


name, age, sex, height and weight from the keyboard and to store them
in a file using read() and write() functions. Again the same file is opened
for reading and displaying the contents of the file on the screen.

CODE :

NIKHIL MATHUR 05214802719


OOPS USING C++ (ETCS-258)

OUTPUT :

NIKHIL MATHUR 05214802719

You might also like