Professional Documents
Culture Documents
For se ng length, we have, It will take -5 and set it into the breadth and s ll
void setLength(int l){ breadth is -5 again. Then what is the benefit of
length = l; wri ng the func ons? Now we will make those
} func ons a li le smart by checking the data
This is the func on for se ng the length of the they are ge ng and then se ng it. Let us
rectangle. This is taking one parameter which is modify those func ons.
the length that we want to set. And inside this,
we assigned the value of ‘l’ to the length void setLength(int l){
variable. For se ng breadth, we have, if(l >= 0)
void setBreadth(int b){ length = l;
breadth = b; else
} length = 0;
Here we assigned the value of ‘b’ to the }
breadth variable. So, these are the se ers that void setBreadth(int b){
will set the given value of length and breadth. if(b >= 0)
Now let us write ge ers. breadth = b;
int getLength(){ else
return length; breadth = 0;
} }
int getBreadth(){
return breadth; Now the setLength func on is valida ng the
} data that it is ge ng. If ‘l’ is greater than 0 then
These are the ge er func ons for length and the only length will be assigned to the length
breadth. Both the func ons are of type ‘int’ as variable otherwise length will be 0. Same with
they are returning an integer value. Now how breadth func on. We can also write a
to use these func ons? Let us see. statement inside setLength or setBreadth
int main(){ func on that will inform us that the data is
invalid or nega ve. Now we have a setLength whatever the data member, if it is length then
and setBreadth func ons which validate the it would getLength, if it is breadth then it will
data and assign it. So, if we write, getBreadth, if it is marks then getMarks, if it is
prize then getPrize. These func ons are called
int main(){ Accessors and all the set func ons are called
Rectangle r; Mutators. Both the func ons se ers and
r.setLength(10); ge ers are known as property func ons.
r.setBreadth(-5); Because data numbers are called property. So,
cout << r.Area() << endl; these types of func ons are called property
} func ons, and the one which is reading the
value is called accessors and the one which is
Here we are passing -5, so the breadth func on wri ng the value is called mutator.
will set the breadth as zero. Next, we have
called the area func on. Then what will we Now few more things, for any data member
get? See length is 10 and breadth is 0 then the that is length or breadth, if you are wri ng both
area will be 0. By using the func on, we have get and set func on then the property is a read
created some condi ons that should be writeable. If you write only accessors or get
followed to assign values to data members. func ons and don’t write a set func on, then it
With this, we will not get any wrong results or is read-only. You cannot write the property you
nega ve results. But s ll, the main func on is cannot change the property so it depends on
not knowing or the user is not knowing that you, which property you want to be read
there is a problem with the nega ve value. So, writeable or only readable. Now let us write
if you want you can write “length or breadth the complete program.
cannot be nega ve” inside the setLength and
setBreadth func on. Next, we want to display Program to Understand Data Hiding in C++:
the length of a rectangle or breadth of a #include <iostream>
rectangle. So, we can say, using namespace std;
class Rectangle
r.getLength(); {
r.getBreadth(); private:
int length;
It will give the value of Length and Breadth and int breadth;
print on the screen. Finally, we have achieved
data hiding. We made the data private and public:
made the func ons public and because we void setLength (int l)
were unable to access the data, we have {
provided some valida ng func ons or if (l >= 0)
intelligence which will take the valid data and length = l;
assign it to those data members. If it is invalid else
then you can print a message that tells the user {
that the data is invalid. length = 0;
cout << "Length cannot be nega ve or
How many func ons we have wri en? zero" << endl;
The setLengh, setBreadth, getLength and }
getbreadth. We have wri en two data
func ons get and set. The ‘get’ will give you the }
value of data members and the ‘set’ will void setBreadth (int b)
change the value of the data members. So, {
if (b >= 0) Why do we need Constructors in C++?
breadth = b; Before understanding what are constructors,
else let’s first understand why we need
{ Constructors in C++. For a be er understanding
breadth = 0; please have a look at the below code.
cout << "Breadth cannot be nega ve or
zero" << endl;
} class Rectangle
{
} private:
int getLength () int length;
{ int breadth;
return length; public:
} void setLength (int l)
int getBreadth () {
{ if (l >= 0)
return breadth; length = l;
} else
int Area () length = 0;
{ }
return length * breadth; void setBreadth (int b)
} {
int Perimeter () if (b >= 0)
{ breadth = b;
return 2 * (length + breadth); else
} breadth = 0;
}; }
int main() int getLength ()
{ {
Rectangle r; return length;
r.setLength (10); }
r.setBreadth (10); int getBreadth ()
cout << "Area: " << r.Area () << endl; {
cout << "Perimeter: " << r.Perimeter () << return breadth;
endl; }
} int Area ()
Output: {
return length * breadth;
Constructors in C++ with Examples }
In this ar cle, I am going to discuss int Perimeter ()
Constructors in C++ with Examples. Please read {
our previous ar cle, where we discussed Data return 2 * (length + breadth);
Hiding in C++ with Examples. At the end of this }
ar cle, you will understand what are };
Constructors and their type as well as their role Here we have a class called Rectangle with
and responsibility in C++ Applica ons with length and breadth as data members as well as
Examples. the accessor and mutator methods for these
two data members. Now we will create an from the market and it has no color. When you
object class Rectangle. placed an order at that me only you said that
I want a white color car or any other color car.
Rectangle r; So, the color of this car is white. It is not like
first you will buy a car and bring it home and
The object of the Rectangle class is created then you give white color.
having length and breadth as shown in the
below image. Coming back to our example, first, we are
crea ng a rectangle object, and then only we
are se ng its length and breadth data
Why do we need Constructors in C++? members. This Philosophically is wrong. When
you booked the car, at that me only you said
Now, we can access all the func ons of class that the car color should be white and then you
Rectangle through object r. Now the problem is should get a white car. That’s it. So, when the
that we have created an object of Rectangle. car is delivered, it is having some color, not
We got length and breadth. But what are the garbage.
values stored in them? There are no values. Did
we ini alize them? No. So they have garbage
values. Then we want to set the length. So, we In the same way, when you are crea ng an
should call the setLength and setBreadth object of Rectangle at that me only, we should
func ons. We are calling these func ons which be able to say that I want the length of 10 and
will take the given value and set them to length a breadth of 5. You don’t have to set it up later.
and breadth data members. This So, this is the philosophy behind that one.
Philosophically is totally wrong. Let’s see how.
We want the length and breadth to be set at
We have created an object of Rectangle. Later the me of construc on of that object. So,
we set the length and breadth data members. what we should have inside the class? What is
There is a problem. What is that? Suppose we visible or publicly accessible? Func ons. So, we
have purchased a rectangle from the market. should have a func on that should
So, when we purchase it, was it not have any automa cally be called when an object is
length and breadth? When we buy something constructed. That func on will take values of
from the market or when we get something length and breadth as parameters and assign
constructed in some factory and when they those values. These func ons are known as
hand over the thing to us then that thing will Constructors in C++.
have some proper es. Suppose if it is a
rectangle then it must have some length and What are Constructors in C++?
breadth. How it can be garbage random length C++ allows the object to ini alize itself when it
and breadth? There must be some length and is created. This automa c ini aliza on of the
breadth, not just garbage. object is known as Constructors. The
constructor in C++ is a block of code that
ini alizes the newly created object.
Let’s understand the same concept with one
more example. Suppose we have a car and
color is the property of the car. A constructor ini alizes an object immediately
upon crea on. It has the same name as the
You have bought a car and the car will have class in which it resides and is syntac cally
some color. What is the color of the car? No similar to a method. Once defined, the
color. How it is possible that you bought a car constructor is called automa cally immediately
while crea ng the object. Constructors have no every class will have some constructor. If we
return type, not even void. don’t provide any constructor explicitly, then
the compiler provides a built-in constructor
So, in simple words, we can define the which is called as default constructor.
constructors in C++ are the special types of
methods of a class that are automa cally Types of Constructors in C++:
executed whenever we create an instance A constructor is a func on that will have the
(object) of that class. The Constructors are same name as the class name. Can we write
responsible for two things. One is the object our own constructor? Yes, we can write our
ini aliza on and the other one is memory own constructors. Then how we can write? We
alloca on. can write three different types of constructors.