Lab 6- Constructors in c++ . IST .pptx

saadpisjes 32 views 14 slides May 05, 2024
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

Constructors in c++ programming language


Slide Content

CONSTRUCTORS

A constructor in C++ is a special ‘MEMBER FUNCTION’ having the same name as that of its class which is used to initialize some valid values to the data members of an object. It is executed automatically whenever an object of a class is created. The only restriction that applies to the constructor is that it must not have a return type or void. It is because the constructor is automatically called by the compiler and it is normally used to INITIALIZE VALUES. The compiler distinguishes the constructor from other member functions of a class by its name which is the same as that of its class. ctorz is an abbreviation of constructor in C+ C++ constructor can be defined as a class in the same way as that of normal member functions and can access any of its data members. The syntax for defining constructor inside the class body is as follows:

class CLASSNAME { ……… public : CLASSNAME([ parameter_list ]) // constructor definition { . . . . . . } . . . . . . . . }; INSIDE THE CLASS

class CLASSNAME  {  . . . . . . . . public:           CLASSNAME ([ parameter_list ]);//Constructor declaration          . . . . . . . . . };  CLASSNAME: :CLASSNAME([ parameter_list ])//Constructor Definition { . . . . . . . . . . . } OUTSIDE THE CLASS

class Wall public: // create a constructor Wall() { // code } };

Here, the function Wall() is a constructor of the class Wall. Notice that the constructor has the same name as the class, does not have a return type, and is public

A constructor to which no arguments are passed is called the Default constructor. It is also called a constructor with no parameters. In the example above, Wall() is a default constructor. Using the default constructor, data members can be initialized to some realistic values in its definition even though no arguments are specified explicitly. C++ Default Constructor

EXAMPLE 1  If we have not defined a constructor in our class, then the C++ compiler will automatically create a default constructor with an empty code and no parameters.

Initialize a student’s data ( university name, batch, roll no) using a default constructor. Use string to get university name; TASK 1

In C++, a constructor with parameters is known as a parameterized constructor. This is the preferred method to initialize member data. C++ Parameterized Constructor

EXAMPLE 2

EXAMPLE 3

Using parameterized constructor, initialize a person’s bank account data ( Account title, Account number, Account total salary ). Hint: Use string to get the account title. TASK 2

1. Using parameterized constructor, initialize the account salary of a person in may , june and july . Using functions calculate total salary. If the salary for each of these month is increased 30 percent, display the new total salary for these months 2. Write a program that defines a constructor to find the maximum and minimum values for 3 numbers HOME TASK
Tags