Unit-IV
Unit-IV
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.
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;
}
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;
Syntax:
const int* x;
#include <iostream>
using namespace std;
int main ()
{
// declare integer variable
int x = 7, y = 10;
// *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'
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);
}
Syntax:
return_type mem_fun() const
{
}
class ABC
{
// define the access specifier
public:
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.
/* 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;
}
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;
}
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;
}
O/P:
Enter the Id of the Car:
101
Enter the name of the Car:
Ferrari
Number of the Marks (1 - 10):
10
#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
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;
}
#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;
}