0% found this document useful (0 votes)
6 views7 pages

Unit 2 C

Uploaded by

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

Unit 2 C

Uploaded by

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

Programming in C++

Unit-2

Constructors and Destructors


In C++, a constructor is a special member function of a class that initializes objects of
that class. It is automatically called when an object of the class is created.
Constructors have the same name as the class and do not have a return type.

1. Default Constructor

A default constructor is used to create an object with default settings. It doesn’t


require any arguments.

Example:

In this example, Simple has a default constructor that initializes number to 0. When
you create an object obj of Simple, it automatically calls this constructor.
2. Parameterized Constructor

A parameterized constructor takes arguments and initializes the object based on


those values.

Example:

Here, Simple has a parameterized constructor that sets number to the value provided
(in this case, 42).
3. Copy Constructor
A copy constructor creates a new object as a copy of an existing object. It’s used
when you need to duplicate an object.

Example:

In this example, obj2 is created as a copy of obj1, using the copy constructor.
Overloading

Overloading in programming refers to the ability to use the same name for different
functions, operators, or constructors within a class, where the specific behavior is
determined by the context in which they are used. This concept is especially prevalent
in object-oriented languages like C++ and Java.

Function Overloading

Function overloading lets you create multiple functions with the same name, but with
different parameter lists.

Example

Explanation

 print(int i): This function is called when you pass an integer.


 print(double d): This function is called when you pass a double.
 print(const std::string& s): This function is called when you pass a string.
Example 2:

Stream:

In C++, a stream is essentially a flow of data, either to or from a device


like the console, a file, or even memory. Streams abstract the reading and
writing of data, so you don’t need to worry about the details of how the
data is transmitted. There are different types of streams, but the most
common ones are for input and output.

 Input Stream (istream): Used for reading data (input).


 Output Stream (ostream): Used for writing data (output).
Common Streams in C++:

 cin: Input stream, usually associated with the keyboard (for user
input).
 cout: Output stream, usually associated with the screen (for
displaying output).
 cerr: Output stream for errors.

Example: Using cin and cout

Here’s an example to illustrate how cin and cout work in a simple C++
program:

Explanation:

1. std::cout is used to print messages to the console (output stream).


1. std::cout << "Enter your name: " prints the text to the
console.
2. std::cin is used to take input from the user (input stream).

1. std::cin >> name; reads the input from the console and stores
it in the variable name.
3. The final std::cout displays the entered data, using the values stored
in name and age.

Key Points:

 Streams are like channels through which data flows (either to or


from your program).
 << is used with cout to send data (output).
 >> is used with cin to receive data (input).

You might also like