Oop Lab Manual 2023-24

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Dr. D. Y.

Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44


Department of Electronics and Telecommunication Engineering

Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi , Pune – 411044.

Department of Electronics and Telecommunication Engineering

OBJECT ORIENTED PROGRAMMING LAB MANUAL

Code: 204197

S.E. E&TC

Academic Year 2023-24

Name: Sania Jamadar


Roll No.:SEETC2362

1
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

INDEX

S.No. Name of the Experiment Page No.

1
Write a program in C++ to sort the numbers in an array using
separate functions for read,
display, sort and swap. The objective of this assignment is to
learn the concepts of input,
output, functions, call by reference in C++.

2 Write a C++ program that illustrates the concept of Function


overloading.

3 Write a program in C++ to implement Stack. Design the class


for stack and the operations to
be performed on stack. Use Constructors and destructors. The
objective of this assignment is
to learn the concepts classes and objects, constructors and
destructors.

4
Write a program in C++ to overload unary operators for
complex classes.

5 Write a program in C++ to perform following operations on


complex numbers Add, Subtract, Multiply, Divide. Use operator
overloading for these operations. The objective of this
assignment is to learn the concepts operator overloading.

2
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

S.No. Name of the Experiment Page No.

6
Write a program in C++ to implement string class. Write
constructors, destructor, Accepts function and Display function

7 Write a program in C++ to Read and Display the information of


Employee Using Multiple Inheritance. Use Basic Info and
Department Info as a base classes of Employee class.

8 Write a C++ program that illustrates run time polymorphism by


using virtual functions.

9
Write a C++ program which use try and catch for exception
handling.

10 Write a C++ program which to implement class and function


template.

11 Virtual Lab 1: Class and Objects in C++


http://vlabs.iitb.ac.in/vlabs-dev/labs/oops/labs/exp2/simulation.p
hp

12 LAB 12 Virtual Lab


Virtual Lab 2: Functions in C++
http://vlabs.iitb.ac.in/vlabs-dev/labs/oops/labs/exp3/simulation.p
hp

3
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

Experiment No: 01

AIM: Write a Simple program on C++

APPARATUS: Computer with Windows Operating System.

Program:

1. Hello World!
2. Call By Value and call By Reference
3. Scope Resolution
4. Constructor

Experiment No: 01

AIM: Write a program in C++ to sort the numbers in an array using separate functions for read,
display, sort and swap. The objective of this assignment is to learn the concepts of input, output,
functions, call by reference in C++.

APPARATUS: Computer with Windows Operating System.

THEORY:
Sorting Algorithms:
⮚ Sorting means arranging a set of data in some order.
⮚ There are different methods that are used to sort the data in ascending or descending order.
⮚ There are different simple sorting methods available which are,
✔ Selection sort,
✔ Bubble sort,
✔ Insertion Sort.
⮚ Also different complex sorting methods available which are,
⮚ Count Sort
⮚ Shell Sort
⮚ Heap Sort
⮚ Merge Sort
⮚ Quick Sort
1) Bubble Sort :
⮚ In this method, to arrange elements in ascending order, to begin with 0th element is compared with 1st element. If it

4
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

is found to be greater than 1st element then they are exchanged.


⮚ Then 1st element is compared with 2nd element, if it is found to be greater, then they are
exchanged. ⮚ In the same way elements are compared with their next element and
interchanged if required.
⮚ This is the first iteration and on completing this iteration the largest element gets placed at the last position. ⮚
Similarly, in second iteration the comparisons are made till last but one element gets placed at second last position in
the list.
⮚ If there are “n” elements then maximum (n-1) iterations needs to be performed.

Example:

Algorithm for Bubble Sort (pseudo code)


Bubble Sort ( )
Description: Here a is an unsorted array having n elements.
1. Repeat For i = 0 to n-1
2. Repeat For j = 0 to n-i-1
3. If (a[j] > a[j+1]) Then
4. Interchange a[j] and a[j+1]
[End of If]
[End of Step 2 For Loop]
[End of Step 1 For Loop]
5. Exit

2) Selection Sort :
⮚ This is the simplest method of sorting. In this method, to sort data in ascending order in first iteration the 0th element
is compared with all other elements.
⮚ If the 0th element is found to be greater than the compared element then they are exchanged. So, after first iteration
the smallest element is placed at 0th position.
⮚ The same procedure is repeated for the 1st element and so on…
⮚ If there are “n” elements then maximum (n-1) iterations needs to be performed.
Example with Explanation:
⮚ Suppose, an array consists of 5 numbers, the selection sort algorithm works as follows.
⮚ In first iteration 0th element 25 is compared with 1st element 17 and since 25 is greater than 17 they are exchanged.
⮚ Now the 0th element 17 is compared with 2nd element 31, but 17 is being less than 31, hence they are not
interchanged. ⮚ This process is repeated till 0th element is compared with rest of the elements. During the comparison
if 0th element is found to be greater than the compared element, then they are interchanged, otherwise not.
⮚ At the end of first iteration, the 0th element holds the smallest number.
⮚ Now, the second iteration starts with 1st element 25. The above process of comparison and swapping is
repeated. ⮚ So if there are “n” numbers then after (n-1) iterations array is sorted.

5
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

Algorithm for Selection Sort (pseudo code)


Selection Sort ( ):
Description: Here A is an unsorted array having N elements.
1. Repeat For I = 0 to N-2
2. Repeat For J = I+1 to N-1
3. If (A[I] > A[J]) Then
4. Interchange A[I] and A[J]
[End of If]
[End of Step 2 For Loop]
5. [End of Step 1 For Loop]
6. Exit

3) Insertion sort:
✔ Insertion sort is implemented by inserting a particular element at the appropriate position.
✔ In this method, the first iteration starts with comparison of 1st element with 0th element.
✔ In second iteration, 2nd element is compared with 0th and 1st element.
✔ In general, in every iteration an element is compared with all elements before it.
✔ During comparison if it is found that the element in question(list/array) can be inserted at suitable position then
space is created for it by shifting the other elements one position to the right and inserting the element at suitable
position. Example:

Explanation:
⮚ In the 1st iteration the 1st element 17 is compared with 0th element 25. Since 17 is smaller than 25, the 0th element
is shifted one position to the right and 17 is inserted at 0th position.
⮚ In the second iteration, the 2nd element 31 & 0th elements 17 are compared. Since 31 is greater than 17, nothing is

6
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

done. Then 2nd element is compared with the 1st element 25. Again no action is taken as 25 is less than 31. ⮚ In the
third iteration, the 3rd element 13 is compared with 0th element 17. Since, 13 is smaller than 17, 13 is inserted at 0th
position in the array and all the element from 0th till 2nd position are shifted to right by one position. ⮚ In the fourth
iteration the 4th element 2 is compared with 0th element 13, since 2 is smaller than 13, the 4th element is inserted at 0th
place in the array and all the elements from 0th till 3rd are shifted right by one position. ⮚ As a result, the array now
becomes a sorted array.

Algorithm for Insertion Sort (pseudo code)


Insertion Sort ( ):
Description: Here A is an unsorted array having N elements.
1. Repeat For I = 1 to N-1
2. Repeat For J = 0 to I
3. if(A[J]>A[I])
4. Repeat For K = I to K>J
5. Interchange A[K] and A[K-1]
6. Set A[K+1] = TEMP
[End of If]
[End of For Loop]
[End of For Loop]
7. Exit

Analysis of Worst Case Complexity of sorting algorithms


Algorithm Bubble Selection Insertion

Comparisons O(n2) O(n2) O(n2)

Swap O(n2) O(n) O(n2)

What Is a Reference?
A reference is an alias; when you create a reference, you initialize it with the name of another object, the target. From that
moment on, the reference acts as an alternative name for the target, and anything you do to the reference is really done to the
target.You create a reference by writing the type of the target object, followed by the reference operator (&), followed by the
name of the reference, followed by an equal sign, followed by the name of the target object. If you have an integer variable
named someInt, you can make a reference to that variable by writing the following: int &rSomeRef = someInt;

PROGRAM:

OUTPUT:

CONCLUSION:

Experiment No: 02

7
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

AIM: Write a C++ program that illustrates the concept of Function overloading

APPARATUS: Computer with Windows Operating System.

THEORY:
When we create two or more members of a class having the same name but different in number or type of parameters,
it is known as C++ overloading. In C++, we can overload: methods, constructors, and indexed properties

What is function overloading in C++?


Function Overloading in C++ can be defined as the process of having two or more member functions of a class with
the same name, but different in parameters. In function overloading, the function can be redefined either by using
different types of arguments or a different number of arguments according to the requirement. It is only through these
differences compiler can differentiate between the two overloaded functions.One of the major advantages of Function
overloading is that it increases the readability of the program because we don’t need to use different names for the
same action again and again.

By changing the Number of Arguments

In this way of function overloading, we define two functions with the same names but a different number of parameters
of the same type. For example, in the below-mentioned program, we have made two add() functions to return the sum
of two and three integers.
// first function definition

int add(int a, int b)


{
cout << a+b;
}

// second overloaded function definition


int add(int a, int b, int c)
{
cout << a+b+c;
}

Here add() function is said to be overloaded, as it has two definitions, one which accepts two arguments and another
which accepts three arguments. Which add() function will be called, depends on the number of arguments.

int main()
{
add(10,20) ; // add() with two parameter will be called
add(10,20,30) ; // add with three parameter will be called
}

#include <iostream>
using nanamespace std;mespace std;
int add(int a, int b)
{
cout << a+b <<endl;
return 0;
}

int add(int a, int b, int c)


{
cout << a+b+c <<endl;
return 0;

8
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

int main()
{

add(20, 40);

add(40, 20, 30);


}
Out put : 60 9020, 30);
}
In the above example, we overload add() function by changing its number of arguments. First, we define an add()
function with two parameters, then we overload it by again defining the add() function but this time with three
parameters. By having different types of Arguments In this method, we define two or more functions with the same
name and the same number of parameters, but the data type used for these parameters are different. For example in this
program, we have three add() function, the first one gets two integer arguments, the second one gets two float
arguments and the third one gets two double arguments.

Advantages of function Overloading in C++


We use function overloading to save the memory space, consistency, and readability of our program. With the use
function overloading concept, we can develop more than one function with the same name Function overloading
shows the behavior of polymorphism that allows us to get different behavior, although there will be some link using
the same name of the function. Function overloading speeds up the execution of the program. Function overloading is
used for code reusability and also to save memory. It helps application to load the class method based on the type of
parameter. Code maintenance is easy.

Disadvantages of function Overloading in C++


Function declarations that differ only by its return type cannot be overloaded with function overloading process.
Member function declarations with the same parameters or the same name types cannot be overloaded if any one of
them is declared as a static member function.

PROGRAM:

OUTPUT:

CONCLUSION:

9
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

Experiment No: 03

AIM:
Write a program in C++ to perform following operations on complex numbers Add, Subtract,
Multiply, Divide, Complex conjugate. Design the class for complex number representation and the
operations to be performed. The objective of this assignment is to learn the concepts classes and
objects

THEORY:
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.
• A Class is a user defined data-type which has data members and member functions.
• 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.

E & TC- SE-SEM-II-OOP (Object Oriented Programming-2020-21


• 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.
An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an
object is created) memory is allocated.
A class is defined in C++ using keyword class followed by the name of class. The body of class is defined inside the curly
brackets and terminated by a semicolon at the end.

10
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

Declaring Objects: When a class is defined, only the specification for the object is defined; no memory or storage is
allocated. To use the data and access functions defined in the class, you need to create objects.
Syntax:
ClassName ObjectName;

Accessing data members and member functions: The data members and member functions of class can be accessed using the
dot(‘.’) operator with the object. For example if the name of object is obj and you want to access the member function with
the name printName() then you will have to write obj.printName() .

Accessing Data Members


The public data members are also accessed in the same way given however the private data members are not allowed to be
accessed directly by the object. Accessing a data member depends solely on the access control of that data member. This
access control is given by Access modifiers in C++. There are three access modifiers : public, private and protected.

Member Functions in Classes


There are 2 ways to define a member function: Inside class definition, outside class definition
To define a member function outside the class definition we have to use the scope resolution:: operator along with class
name and function name.

Problem Description
We have to input the real and imaginary parts of two complex numbers separately and add them using classes and objects
in C++. Problem Solution
In this problem, we are going to create a function that adds two complex numbers. First the user has to provide real and
imaginary parts as input for both the complex numbers. After that the real part of first complex number will be added with
the real part of second one, similarly the imaginary part of first complex number will be added with the imaginary part of
second complex number. Program/Source Code
Here is source code of the C++ Program to add two Complex Numbers using Classes and Objects. The program is
successfully compiled and tested using Codeblocks gnu/gcc compiler on Windows 10. The program output is also shown
below
. /* C++ Program to add two Complex Numbers */
#include<iostream>
using namespace std;
class Complex{
public:
int real;
int imag;
/* Function to set the values of
* real and imaginary part of each complex number
*/
void setvalue()
{
cin>>real;

11
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

cin>>imag;
}
/* Function to display the sum of two complex numbers */
void display()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
/* Function to add two complex numbers */

void sum(Complex c1, Complex c2)


{
real=c1.real+c2.real;
imag=c1.imag+c2.imag;
}
};
int main()
{
Complex c1,c2,c3;
cout<<"Enter real and imaginary part of first complex number"<<endl; c1.setvalue();
cout<<"Enter real and imaginary part of second complex number"<<endl; c2.setvalue();
cout<<"Sum of two complex numbers is"<<endl;
c3.sum(c1,c2);
c3.display();
return 0;
}

1. Program Explanation
2. Here in this program we have created a few functions.

1. setvalue()
This function allows us to take input for real and imaginary parts of both the complex numbers. To take in real and
imaginary part as an input for first complex number we have called this function using an object c1. So, this function sets
the real and imag values for first complex number. Similarly to set the values of real and imaginary part of second complex
number, we can call it using a new object c2.
2. sum()
This function takes in two complex numbers as a parameter. Using this function we have added the real parts of both the
complex numbers together and stored them into variable real which is the real part of the resultant complex number.
Similarly we have added the imaginary parts of both the complex numbers and stored the result into variable imag which is
the imaginary part of the resultant complex number.
3. display()
We have called this function using an object for the resultant complex number, to display the real and imaginary parts of the
resultant complex number obtained after addition.
Input and Out Put:
1. Enter real and imaginary part of first complex number
56
Enter real and imaginary part of second complex number
13
Sum of two complex numbers is
6+9i
2. Enter real and imaginary part of first complex number
-4 6
Enter real and imaginary part of second complex number
12
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

5 -3
Sum of two complex numbers is
1+3i
3. Enter real and imaginary part of first complex number
-5 -6
Enter real and imaginary part of second complex number
-3 -5
Sum of two complex numbers is
-8+-11i

PROGRAM:

OUTPUT:

CONCLUSION:

Experiment No. :4

AIM: Write a program in C++ to implement Stack. Design the class for the stack and the operations to be
performed on the stack. Use Constructors and destructors.

THEORY:
Stacks: Concept, Array representation of stack
✓ A stack is a data structure in which addition of new element or deletion of an existing element always takes place at
the same end. This end is often known as top of stack.
✓ When an item is added to a stack, the operation is called as push.
✓ And when an item is removed from stack the operation is called as pop.
✓ Stack is called Last-In-First-Out (LIFO) list.
✓ Stack can be made by using either array (or array of structure) or linked list.
✓ If the elements are continuously added to the stack it grows at one end.

13
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

E & TC- SE-SEM-II-OOP (Object Oriented Programming-2020-21

Figure 1: pictorial representation of stack after inserting elements


(top can be initialized to -1/0 if implemented by using array or initialized to NULL if implemented by using
LL) ✓ On deletion of elements the stack shrinks at the same end, as the element at the top gets removed.

Figure 2: pictorial representation of stack after deleting elements

Stack as ADT
✓A stack is generally implemented with two basic operations- push and pop.
✓ And there are various operations that may be implemented with stack. These are listed below.
Operation Description
push() Allows adding an element at the top of the stack.

pop() Allows to remove an element from the top of the stack.

isempty() Checks if stack is empty.

isfull() Checks if stack is Full.

search() Checks whether a particular element is present in stack.

printel() Prints the elements of stack.

Constructors:
➢ A constructor is a special member function whose task is to initialize the object of
its class. ➢ It is special because its name is same as that of class name.
➢ The constructor is invoked whenever an object of its associated class is created.
➢ It is called constructor because it constructs the values of data members of the class.
➢ The constructor function has some special characteristics. These are
➢ They should be declared in public section.
➢ They are invoked automatically when the objects are created.
➢ They do not have return types not even void and therefore they cannot return values.

14
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

➢ They cannot be inherited, though a derived class can called base class constructor.
➢ Like other c++ functions, they can have default arguments.
➢ Constructors cannot be virtual.
➢ We cannot refer to their addresses.
➢ They make implicit calls to the operator new and delete when memory allocation is required ➢
When constructor is declared for a class, initialization of the class object becomes mandatory. ➢
Constructor can be of different types: default constructor, parameterized constructor and copy
constructor. ➢ Example,
Class code
{
int id;
E & TC- SE-SEM-II-OOP (Object Oriented Programming-2020-21
public:
code(){}
code(int a)
{
id=a;
}
code(code &x)
{
Id=x.id;
}
};
1. Copy constructor is used to declare & initialize an object from another object. The process of initializing through a
copy constructor is known as copy initialization.

Destructors:
A destructor is used to destroy the objects that have been created by a constructor.
Like constructor destructor is a member function whose name is same as that of class name but proceeded by a tilde
(~). A destructor never takes any argument nor it does return any value.
It will be invoked implicitly by the compiler upon exit from a program ( or block or function) to clean up storage that is no
longer accessible.

PROGRAM:

OUTPUT:

CONCLUSION:

15
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

Experiment No. :5

AIM:
Write a program in C++ to perform following operations on complex numbers Add, Subtract,
Multiply, Divide. Use operator overloading for these operations.

THEORY:
C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called
function overloading and operator overloading respectively.An overloaded declaration is a declaration that is declared with
the same name as a previously declared declaration in the same scope, except that both declarations have different
arguments and obviously different definition (implementation).
When you call an overloaded function or operator, the compiler determines the most appropriate definition to use, by
comparing the argument types you have used to call the function or operator with the parameter types specified in the
definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution.
Operators Overloading in C++
You can redefine or overload most of the built-in operators available in C++. Thus, a programmer can use operators with
user defined types as well.Overloaded operators are functions with special names: the keyword "operator" followed by the
symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter
list. Box operator+(const Box&);
Declares the addition operator that can be used to add two Box objects and returns final Box object. Most overloaded
operators may be defined as ordinary non-member functions or as class member functions. In case we define above function
as non-
member function of a class then we would have to pass two arguments for each operand
as follows − Box operator+(const Box&, const Box&);
Following is the example to show the concept of operator over loading using a member function. Here an object is
passed as an argument whose properties will be accessed using this object, the object which will call this operator
can be accessed using this operator as explained below –

#include <iostream>
using namespace std;

class Box {
public:
double getVolume(void) {
return length * breadth * height;
}
void setLength( double len ) {
16
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

length = len;
}
void setBreadth( double bre ) {
breadth = bre;
}
void setHeight( double hei ) {
height = hei;
}

// Overload + operator to add two Box objects.


Box operator+(const Box& b) {
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}

private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};

// Main function for the program


int main() {
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
Box Box3; // Declare Box3 of type Box
double volume = 0.0; // Store the volume of a box here

// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);

// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);

// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;

// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;

// Add two object as follows:


Box3 = Box1 + Box2;
// internally the above line is converted in to “Box.operator+(Box2);”
// volume of box 3
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;

return 0;
}

When the above code is compiled and executed, it produces the following result −
Volume of Box1 : 210

17
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

Volume of Box2 : 1560


Volume of Box3 : 5400

Overloadable/Non-overloadableOperators
Following is the list of operators which can be overloaded −
+ - * / % ^

& | ~ ! , =

< > <= >= ++ --

<< >> == != && ||

+= -= /= %= ^= &=

|= *= <<= >>= [] ()

-> ->* new new [] delete delete []

Following is the list of operators, which can not be overloaded −


:: .* . ?:

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 sometime they can be used as postfix as well like obj++ or obj--.

PROGRAM:

OUTPUT:

CONCLUSION:

18
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

Experiment No: 06

AIM:

Write a program in C++ to implement string class.Write constructor,destructor,accepts


functions and display data.

THEORY:

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.

· A Class is a user defined data-type which has data members and member functions.

· 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.

An Object is an instance of a Class. When a class is defined, no memory is allocated but when
it is instantiated (i.e. an object is created) memory is allocated.

Defining Class and Declaring ObjectsA class is defined in C++ using keyword class followed
by the name of class. The body of class is defined inside the curly brackets and terminated by
a
19
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

semicolon at the end.

Constructor and destructor:

Constructors are special class functions which performs initialization of every object. The
Compiler calls the Constructor whenever an object is created. Constructors initialize values
to object members after storage is allocated to the object.

Whereas, Destructor on the other hand is used to destroy the class object.

Accept function:

Now that we have the class, we can create objects from it, but before we create the object,
let’s first define our functions. We can define functions either within the class (inline member
functions) or outside the class (non-inline member functions).

For example, in the class person, the function void get_details(void) is inline as the function

has been defined within the class.

20
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

The function prototype void display_details(void); can be defined outside the class using the

scope resolution operator::, that specifies the class to which the member being declared

belongs:

void person::display_details(void);

cout<<” NAME: “<<name<<endl;

cout<<” AGE: “<<age<<endl;

Display:

int main()
{
person p;
p.get_details();
p.display_details();
return 0;
}

PROGRAM:

OUTPUT:

21
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

Conclusion:

22
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

Experiment No. :11

Virtual Lab 1: Classes and Objects

AIM:

Theory:

Simulation:

Result

23
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

24
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering

Experiment No. :12

Virtual Lab 2: Functions

AIM:

Theory:

Simulation:

Result

25

You might also like