Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Hands-On C++ Practice Notes for Coders part 2

Rating
-
Sold
-
Pages
15
Uploaded on
20-12-2024
Written in
2024/2025

C++ is a general-purpose programming language known for its efficiency, flexibility, and performance. It was developed by Bjarne Stroustrup in 1983 as an extension of the C programming language, introducing object-oriented programming (OOP) features while retaining the power and speed of C.

Show more Read less
Institution
Course

Content preview

Unit III
1. Constructors
■ Special member function which enables an object to initialize itself when it is created
■ Its name is same as the class name
■ Constructor definition outside class:
class integer
{ int m,n;
public:
integer(void);
……….
};
integer :: integer (void)
{ m=0; n=0;}
■ Constructor definition within class:
class integer
{ int m,n;
public:
integer(void)
{ m=0; n=0;}
……….
};
Characteristics of constructor:
■ They should be declared in the public section
■ They are invoked automatically when the objects are created
■ They do not have return types, not even void and therefore, and they cannot return
values
■ They cannot be inherited, though a derived class can call the base class
■ They can have default arguments
■ Constructors cannot be virtual functions

2. Parameterized Constructors
class integer
{ int m,n;
public:
integer (int x, int y);
……….
};
integer :: integer (int x, int y)
{
m=x;
n=y;
}
■ Explicit constructor call: integer int1=integer(1,2);
■ Implicit constructor call: integer int1(1,2);

3. Multiple Constructors in a Class
class code
{ int id;
public: code( ){ id=5;}
code (int a) { id=a; }
code( int a, int b) {id=a+b; }
};
In this class ‘code’ we have three constructors:
Page | 1

, i) code(): This is a constructor with no parameters.
ii) code(int a): This is a constructor with one parameter.
iii) code(int a, int b): This is a constructor with two parameters.
Hence, multiple constructors may be used in a class as shown in the above code.

4. Questions
Q.1) What is a constructor? Why is it used?
Q.2) What are the rules for creating a constructor?
Q.3) How can parameterized constructors be implemented in C++?
Q.4) How can multiple constructors be implemented in C++?

5. Constructors with Default Arguments
class Demo
{ private:
int X,Y;
public:
Demo()
{ X = 0;
Y = 0;
cout << endl << "Constructor Called";
}
Demo(int X, int Y=20)
{
this->X = X;
this->Y = Y;

cout << endl << "Constructor Called";
}
void putValues()
{
cout << endl << "Value of X : " << X;
cout << endl << "Value of Y : " << Y << endl;
}
};

void main()
{
clrscr();
Demo d1= Demo(10);
cout << endl <<"D1 Value Are : ";
d1.putValues();

Demo d2= Demo(30,40);

cout << endl <<"D2 Value Are : ";
d2.putValues();
getch();
}

Output:
Constructor Called
D1 Value Are :
Value of X : 10
Value of Y : 20
Page | 2

Written for

Institution
Course

Document information

Uploaded on
December 20, 2024
Number of pages
15
Written in
2024/2025
Type
Class notes
Professor(s)
I don\\\'t know
Contains
All classes

Subjects

$8.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
myraaa9900

Also available in package deal

Get to know the seller

Seller avatar
myraaa9900 Barkatullah university
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
6
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions