0% found this document useful (0 votes)
33 views6 pages

Experiment 6 - 7 String

The document outlines an academic assignment for a C++ programming course focused on implementing a string class, including writing constructors, destructors, and operator overloading. It details the objectives, software requirements, and theoretical background on constructors in C++. Additionally, it highlights the differences between C++ strings and character arrays, along with assignment tasks and important notes regarding submission and plagiarism.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views6 pages

Experiment 6 - 7 String

The document outlines an academic assignment for a C++ programming course focused on implementing a string class, including writing constructors, destructors, and operator overloading. It details the objectives, software requirements, and theoretical background on constructors in C++. Additionally, it highlights the differences between C++ strings and character arrays, along with assignment tasks and important notes regarding submission and plagiarism.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Object Oriented Programming

S.E. (E&TC) Sem. - II Academic Year-2022-2023

PVG's College of Engineering and Technology & G. K. Pate (Wani) Institute of


Management, Pune-411009

Accredited by NAAC with Grade “A”


Department of Electronics and Telecommunication
Engineering

Experiment No. 6 : Write a program in C++ to implement string class. Write


constructor’s, destructors accept the function and display the function.

Experiment No. 7 : Write a program in C++ to implement string class. Write


constructor’s, destructors, Accept the function and display the function.
To overload =overload so as call copy constructor.

Name: ​ Class: S.E. ​ ​ Division: I / II


Roll No:​ ​ ​ ​ ​ Date of Submission:
Marks Obtained: / 20 Signature of the subject teacher:
___________________________________________________________________________
Aim:
1.​ Write a program in C++ to implement string class. Write constructor’s,
destructors Accept the function and display the function.

2.​ Write a program in C++ to implement string class. Write constructor’s,


destructors, Accept the function and display the function. To overload =
operator so as call copy constructor.

Objective: To understand the concept of string class and its manipulation functions in
C++.

To learn the concepts of string class and copy constructor.

Mapped Outcome: After completing the experiment, the students will be able to:
1.​ constructor’s, destructors accept the function and display the function .
2.​ Use pointer

Software tools required:


Software : Quincy, Eclipse, Turbo C++

Say No to Plagiarism and Yes to AcknowledgementsGreen Revolution No Pollution


VUG/DTV/UAW
Object Oriented Programming
S.E. (E&TC) Sem. - II Academic Year-2022-2023

Theory: Constructor is called by the compiler whenever the object of the class is

created, it allocates the memory to the object and initializes class data members.
A destructor is called by the compiler when the object is destroyed and its main
function is to deallocate the memory of the object. A Constructor is a special
member function of a class and shares the same name as of class, which
means the constructor and class have the same name. Constructor is called by
the compiler whenever the object of the class is created, it allocates the
memory to the object and initializes class data members by default values or
values passed by the user while creating an object. Constructors don’t have
any return type because their work is to just create and initialize an object.

Syntax of Constructor:
class scaler {
public:
// Constructor
scaler() {
// Constructor body.
}
};

Characteristics of Constructors in C++

●​ A constructor can be made public, private, or protected per our program's


design. Constructors are mostly made public, as public methods are
accessible from everywhere, thus allowing us to create the object of the
class anywhere in the code. When a constructor is made private, other
classes cannot create instances of the class. This is used when there is no
need for object creation. Such a case arises when the class only contains
static member functions(i.e., the functions which are independent of any
class object and can be accessed using a class name with scope resolution
operator, i.e. ::).
●​ A constructor in C++ cannot be inherited. However, a derived class can
call the base class constructor. A derived class(i.e., child class) contains

Say No to Plagiarism and Yes to AcknowledgementsGreen Revolution No Pollution


VUG/DTV/UAW
Object Oriented Programming
S.E. (E&TC) Sem. - II Academic Year-2022-2023

all members and member functions(including constructors) of the base


class.
●​ Constructor functions are not inherited, and their addresses cannot be
referenced.
●​ The constructor in C++ cannot be virtual. A virtual table(also
called vtable) is made for each class having one or more virtual functions.
Virtual functions ensure that the correct function is called for an object
regardless of the type of reference used for the function call. Whenever
an object is created of such a class, it contains a ‘virtual-pointer’ that
points to the base of the corresponding vtable. Whenever there is a virtual
function call, the vtable refers to the function address.

But when a class constructor is executed, there is no virtual table created yet in
the memory, meaning no virtual pointer is defined yet. As a result, a constructor
cannot be declared virtual.

Types of Constructors in C++

There are four types of constructors in C++:

1.​ Default Constructors


2.​ Parameterized Constructors
3.​ Copy Constructors
4.​ Dynamic Constructors

1. Default Constructor:

A constructor which does not receive any parameters is called a Default


Constructor or a Zero Argument Constructor. Every class object is initialized
with the same set of values in the default constructor.

Even if a constructor is not defined explicitly, the compiler will provide a


default constructor implicitly.
Syntax:
//default constructor without any arguments
Employee :: Employee()
their aliases (reference variables) a and b.

Say No to Plagiarism and Yes to AcknowledgementsGreen Revolution No Pollution


VUG/DTV/UAW
Object Oriented Programming
S.E. (E&TC) Sem. - II Academic Year-2022-2023

2. Parameterized Constructor

Using the default constructor, it is impossible to initialize different objects with


different initial values. What if we need to pass arguments to constructors which
are needed to initialize an object? There is a different type of constructor called
Parameterized Constructor to solve this issue.

A Parameterized Constructor is a constructor that can accept one or more


arguments. This helps programmers assign varied initial values to an object at
the creation time.
3. Copy Constructor

A Copy constructor in C++ is a type of constructor used to create a copy of an


already existing object of a class type. The compiler provides a default Copy
Constructor to all the classes. A copy constructor comes into the picture
whenever there is a need for an object with the same values for data members as
an already existing object. A copy constructor is invoked when an existing
object is passed as a parameter.
Syntax:
//Copy constructor
Employee :: Employee(Employee &ptr)

Difference between C++ string and Character Array


Although both the C-style character string and the string class are used for
string representation, there are a few differences. Some of the primary
differences are:
●​ Character array is an array, but a string is a class that defines an object.
●​ Character array size is pre-allocated statically. Hence, extra memory is
not available during runtime, and it waste the memory that is not used.
Since string size is not, pre-allocated there is no wastage, and extra
memory is available during runtime
●​ Character array has the risk of array decay ,but that is not with the case.

Say No to Plagiarism and Yes to AcknowledgementsGreen Revolution No Pollution


VUG/DTV/UAW
Object Oriented Programming
S.E. (E&TC) Sem. - II Academic Year-2022-2023

●​ String are slower than Character array when it comes to implementation.


●​ The string class offers more in built functions to work with and
manipulate strings

Procedure:
1.​ Draw UML diagram
2.​ Compile the written code.

Results: Attach the print of code and output of program.

Conclusion:……………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
………………
………………………………………………………………………………………………
Remarks by Subject teacher about correctness and improvements:

…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
Acknowledgement: My sincere thanks
to………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………

Reference:
1. E Balagurusamy, “Programming with C++”, Tata McGraw Hill, 3rd Edition.
2. Robert Lafore, “Object Oriented Programming in C++”, Sams Publishing, 4th Edition.

Say No to Plagiarism and Yes to AcknowledgementsGreen Revolution No Pollution


VUG/DTV/UAW
Object Oriented Programming
S.E. (E&TC) Sem. - II Academic Year-2022-2023

3. www.programiz.com

Assignment:
1.​ Write a C++ program to pass a string to the function.
2.​ Write a C++ program to concatenate two string using unary operator overloading
Important Notes:

Note1. Following points should be considered while writing the conclusion: Difference
between call by reference in C and C++.
Note2.Print all the documents back to back on every page. Save pages to save trees.
Note3.Printing on single side will reduce the marks
Note4.Plagiarism of any form is strictly prohibited and attract towards reduction in marks
Note4.Submit the assignments well in time i.e. within four days of performing the
experiment to get maximum marks.

Sign Submission Attendance Submission Understanding Total


Date (10) (5) (5) (20)

Say No to Plagiarism and Yes to AcknowledgementsGreen Revolution No Pollution


VUG/DTV/UAW

You might also like