0% found this document useful (0 votes)
4 views

Unit-IV

Uploaded by

sk6684638
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit-IV

Uploaded by

sk6684638
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Const Keyword

const keywords used to define the constant value that cannot change during program execution.
It means once we declare a variable as the constant in a program, the variable's value will be
fixed and never be changed.

 Use const variable


 Use const with pointers
 Use const pointer with variables
 Use const with function arguments
 Use const with class member functions
 Use const with class data members
 Use const with class objects

1. Const variable
It is a const variable used to define the variable values that never be changed during the
execution of a program. And if we try to modify the value, it throws an error.
Syntax
const data_type variable_name;

example
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
// declare the value of the const
const int num = 22;
num = num + 10;
return 0;
}

O/P: Compile-time error because we update the assigned value of the num 22 by
10.

#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
// declare variables
const int x = 20;
const int y = 25;
int z;
// add the value of x and y
z = x + y;
cout << " The sum of the x and y is: " << z << endl;
return 0;
}

O/P: The sum of the x and y is: 42

2. Constant pointer
To create a const pointer, we need to use the const keyword before the pointer's name.
We cannot change the address of the const pointer after its initialization.
#include <iostream>
using namespace std;
int main ()
{
// declaration of the integer variables
int x = 10, y = 20;

// use const keyword to make constant pointer


int* const ptr = &x; // const integer ptr variable point address to the variable x

// ptr = &y; // now ptr cannot changed their address


*ptr = 15; // ptr can only change the value
cout << " The value of x: " << x << endl;
cout << " The value of ptr: " << *ptr << endl;
return 0;
}

O/P: The value of x: 15


The value of ptr: 15
3. Pointer to constant variable
It means the pointer points to the value of a const variable that cannot change.

Syntax:
const int* x;

#include <iostream>
using namespace std;
int main ()
{
// declare integer variable
int x = 7, y = 10;

const int *ptr = &x; // here x become constant variable


cout << " \n The initial value of ptr:" << *ptr;
cout << " \n The value of x: " <<x;

// *ptr = 15; It is invalid; we cannot directly assign a value to the ptr variable
ptr = &y; // here ptr variable pointing to the non const address 'y'

cout << " \n The value of y: " <<y;


cout << " \n The value of ptr:" << *ptr;
return 0;
}

O/P: The initial value of ptr: 7


The value of x: 7
The value of y: 10
The value of ptr: 10

4. Constant function Arguments


We can declare the function arguments as the constant argument using the const
keyword. And if the value of function arguments is declared const, it does not allow
changing its value.

Syntax:
return_type fun_name (const int x)
{
}

#include <iostream>
using namespace std;
// create an integer Test() function contains an argument num
int Test (const int num)
{
// if we change the value of the const argument, it thwrows an error.
// num = num + 10;
cout << " The value of num: " << num << endl;
return 0;
}
int main ()
{
// call function
Test(5);
}

O/P: The value of num: 5

5. Const Member function of class


A const is a constant member function of a class that never changes any class data
members, and it also does not call any non-const function. It is also known as the read-
only function. We can create a constant member function of a class by adding the const
keyword after the name of the member function.

Syntax:
return_type mem_fun() const
{
}

class ABC
{
// define the access specifier
public:

// declare int variables


int A;
// declare member function as constant using const keyword
void fun () const
{
A = 0; // it shows compile time error
}
};

int main ()
{
ABC obj;
obj.fun();
return 0;
}
O/P:
compilation error because the fun() function is a const member function of class ABC,
and we are trying to assign a value to its data member 'x' that returns an error.

6. Constant Data Members of class


Data members are like the variable that is declared inside a class, but once the data
member is initialized, it never changes, not even in the constructor or destructor. The
constant data member is initialized using the const keyword before the data type inside
the class. The const data members cannot be assigned the values during its declaration;
however, they can assign the constructor values.

/* create a program to demonstrate the data member using the const keyword in C++.
*/
#include <iostream>
using namespace std;
// create a class ABC
class ABC
{

public:
// use const keyword to declare const data member
const int A;
// create class constructor
ABC ( int y) : A(y)
{
cout << " The value of y: " << y << endl;
}
};
int main ()
{
ABC obj( 10); // here 'obj' is the object of class ABC
cout << " The value of constant data member 'A' is: " << obj.A << endl;
// obj.A = 5; // it shows an error.
// cout << obj.A << endl;
return 0;
}

O/P: The value of y: 10


The value of constant data member 'A' is: 10
7. Constant Objects
When we create an object using the const keyword, the value of data members can
never change till the life of the object in a program. The const objects are also known
as the read-only objects.

Syntax:
const class_name obj_name;

#include <iostream>
using namespace std;
class ABC
{
public:
// define data member
int A;
// create constructor of the class ABC
ABC ()
{
A = 10; // define a value to A
}
};
int main ()
{
// declare a constant object
const ABC obj;
cout << " The value of A: " << obj.A << endl;
// obj.A = 20; // It returns a compile time error
return 0;
}

O/P: The value of A: 10


Static data member
When we define the data member of a class using the static keyword, the data members are
called the static data member. A static data member is similar to the static member function
because the static data can only be accessed using the static data member or static member
function. And, all the objects of the class share the same copy of the static member to access
the static data.
Syntax:
static data_type data_member;
#include <iostream>
#include <string.h>
using namespace std;
// create class of the Car
class Car
{
private:
int car_id;
char car_name[20];
int marks;

public:
// declare a static data member
static int static_member;

Car()
{
static_member++;
}

void inp()
{
cout << " \n\n Enter the Id of the Car: " << endl;
cin >> car_id; // input the id
cout << " Enter the name of the Car: " << endl;
cin >> car_name;
cout << " Number of the Marks (1 - 10): " << endl;
cin >> marks;
}

// display the entered details


void disp ()
{
cout << " \n Id of the Car: " << car_id;
cout << "\n Name of the Car: " << car_name;
cout << " \n Marks: " << marks;
}
};
// initialized the static data member to 0
int Car::static_member = 0;
int main ()
{
// create object for the class Car
Car c1;
// call inp() function to insert values
c1. inp ();
c1. disp();
//create another object
Car c2;
// call inp() function to insert values
c2. inp ();
c2. disp();
cout << " \n No. of objects created in the class: " << Car :: static_member <<endl;
return 0;
}

O/P:
Enter the Id of the Car:
101
Enter the name of the Car:
Ferrari
Number of the Marks (1 - 10):
10

Id of the Car: 101


Name of the Car: Ferrari
Marks: 10

Enter the Id of the Car:


205
Enter the name of the Car:
Mercedes
Number of the Marks (1 - 10):
9

Id of the Car: 205


Name of the Car: Mercedes
Marks: 9
No. of objects created in the class: 2
Static Member Functions
The static member functions are special functions used to access the static data members or
other static member functions. A member function is defined using the static keyword. A static
member function shares the single copy of the member function to any number of the class'
objects. We can access the static member function using the class name or class' objects. If the
static member function accesses any non-static data member or non-static member function, it
throws an error.
Syntax:
class_name::function_name (parameter);

#include <iostream>
using namespace std;
class Note
{
// declare a static data member
static int num;

public:
// create static member function
static int func ()
{
return num;
}
};
// initialize the static data member using the class name and the scope resolution operator
int Note :: num = 5;

int main ()
{
// access static member function using the class name and the scope resolution
cout << " The value of the num is: " << Note:: func () << endl;
return 0;
}

O/P:
The value of the num is: 5
Polymorphism

Compile time polymorphism Run time polymorphism


The function to be invoked is known at the The function to be invoked is known at the run
compile time. time.
It is also known as overloading, early binding It is also known as overriding, Dynamic binding
and static binding. and late binding.
Overloading is a compile time polymorphism Overriding is a run time polymorphism where
where more than one method is having the more than one method is having the same name,
same name but with the different number of number of parameters and the type of the
parameters or the type of the parameters. parameters.
It is achieved by function overloading and It is achieved by virtual functions and pointers.
operator overloading.
It provides fast execution as it is known at the It provides slow execution as it is known at the
compile time. run time.
It is less flexible as mainly all the things It is more flexible as all the things execute at the
execute at the compile time. run time.
Operators Overloading
Operator overloading is a compile-time polymorphism in which the operator is overloaded to
provide the special meaning to the user-defined data type.
Operator that cannot be overloaded
 Scope operator (::)
 Sizeof
 member selector(.)
 member pointer selector(*)
 ternary operator(?:)
Syntax:
return_type class_name : : operator op(argument_list)
{
// body of the function.
}

Rules
 Existing operators can only be overloaded, but the new operators cannot be
overloaded.

 The overloaded operator contains at least one operand of the user-defined data type.
 We cannot use friend function to overload certain operators. However, the member
function can be used to overload those operators.

 When unary operators are overloaded through a member function take no explicit
arguments, but, if they are overloaded by a friend function, takes one argument.

 When binary operators are overloaded through a member function takes one explicit
argument, and if they are overloaded through a friend function takes two explicit
arguments.

#include <iostream>
using namespace std;
class Test
{
private:
int num;
public:
Test(): num(8){}
void operator ++() {
num = num+2;
}
void Print() {
cout<<"The Count is: "<<num;
}
};
int main()
{
Test tt;
++tt; // calling of a function "void operator ++()"
tt.Print();
return 0;
}

O/P: The Count is: 10

#include <iostream>
using namespace std;
class A
{

int x;
public:
A(){}
A(int i)
{
x=i;
}
void operator+(A);
void display();
};

void A :: operator+(A a)
{

int m = x+a.x;
cout<<"The result of the addition of two objects is : "<<m;

}
int main()
{
A a1(5);
A a2(4);
a1+a2;
return 0;
}

O/P: The result of the addition of two objects is : 9

You might also like