Files 3 2021 September NotesHubDocument 1632938285
Files 3 2021 September NotesHubDocument 1632938285
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.
Overall Comments:
Signature
LAB-1
15-03-21
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
STOP
OUTPUT :
EXPERIMENT 2.
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:
CODE :
OUTPUT :
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:
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 :
OUTPUT :
LAB-2
22-03-21
EXPERIMENT 4.
AIM : Write a program to find the area of triangle, circle and rectangle using
the concept of function overloading.
THEORY:
CODE :
OUTPUT :
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:
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.
CODE :
OUTPUT :
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 :
CODE :
OUTPUT :
LAB-3
26-04-21
EXPERIMENT 7.
THEORY :
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.
CODE :
NIKHIL MATHUR 05214802719
OOPS USING C++ (ETCS-258)
OUTPUT :
EXPERIMENT 8.
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.
CODE :
OUTPUT :
EXPERIMENT 9.
AIM : Write a program to enter any number and find its factorial using the
constructor.
THEORY :
C++ Constructor :
CODE :
OUTPUT :
EXPERIMENT 10.
THEORY :
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.
CODE :
OUTPUT :
LAB-4
03-05-21
EXPERIMENT 11.
THEORY :
CODE :
OUTPUT :
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 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
CODE :
OUTPUT :
EXPERIMENT 13.
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 :
OUTPUT :
LAB-5
17-05-21
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 :
OUTPUT :
EXPERIMENT 15.
THEORY :
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.
CODE :
OUTPUT :
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 :
OUTPUT :
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 :
OUTPUT :
LAB-6
24-05-21
EXPERIMENT 18.
THEORY :
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 :
OUTPUT :
EXPERIMENT 19.
THEORY :
CODE :
OUTPUT :
EXPERIMENT 20.
THEORY :
CODE :
OUTPUT :
EXPERIMENT 21.
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 :
OUTPUT :
LAB-7
31-05-21
EXPERIMENT 22.
THEORY :
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.
CODE :
OUTPUT :
EXPERIMENT 23.
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) );
}
CODE :
OUTPUT :
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
};
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 −
CODE :
OUTPUT :
LAB-8
07-06-21
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
};
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 −
CODE :
OUTPUT :
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 :
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.
OUTPUT :
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 :
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.
CODE :
OUTPUT :
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 :
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.
CODE :
OUTPUT :
LAB-9
14-06-21
EXPERIMENT 29.
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 :
OUTPUT :
EXPERIMENT 30.
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’.
CODE :
OUTPUT :
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 :
OUTPUT :
EXPERIMENT 32.
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 :
OUTPUT :
LAB-10
21-06-21
EXPERIMENT 33.
THEORY :
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.
CODE :
OUTPUT :
EXPERIMENT 34.
AIM : Write a program to read a set of lines from keyboard and store it
on a specified file.
CODE :
OUTPUT :
EXPERIMENT 35.
AIM : Write a program to read a text file and display its contents on the
Screen.
CODE :
OUTPUT :
EXPERIMENT 36.
CODE :
OUTPUT :
EXPERIMENT 37.
CODE :
OUTPUT :