C++Programs(22-12-2020)
C++Programs(22-12-2020)
Assignment(Operator Overloading)
Write a program that overloads > operator to check greater string
between 2 specified strings.
class c1
{
char a[30];
}
============================================
=====================
Constant Variables in C++
1. If you make any variable as constant, using const keyword, you cannot change its value.
2. Also, the constant variables must be initialized while they are declared.
eg:-
const int a=10(integer constant)
const char c=’D’(char constant)
eg:- const char arr[50]=”Ajmer”(string constant)
Program 230
//230.Constant variable
#include<conio.h>
#include<iostream.h>
void main()
{
const int a=10;
//we cannot change value of a during the program
//a=60; //error
clrscr();
cout<<endl<<"Value of a is="<<a;
getch();
}
/*Output
Value of a is=10
*/
1|Page 22-12-2020
C++ Programming
int getValue() const{return value;}
Program 231
//231.Const Member function in c++
#include<iostream.h>
#include<conio.h>
class pixel
{
private:
int a;
public:
pixel()
{
a=0;
}
int geta()const
{
//a++;
return a;
}
void seta(int a1) //Error we cannot change
{ a=a1;} value of data member
void display() const because function is constant
{
//a=a+10; //error
cout<<endl<<"a is="<<a;
}
~pixel()
{
cout<<endl<<"Destructor called";
}
};
void main() a=0 10 60
{
pixel p1; p1 object
clrscr();
p1.seta(10);
cout<<endl<<"Details of p1 are=";
p1.display(); //10
p1.seta(60);
cout<<endl<<"Value of p1 is="<<p1.geta(); //60
getch();
}
/*Output
Details of p1 are=
a is=10
Value of p1 is=60 */
Program 232
//232.Example of declaring Const function out of the class
2|Page 22-12-2020
C++ Programming
#include<iostream.h>
#include<conio.h>
class pixel
{
private:
int a;
int b;
public:
pixel()
{
a=0;
b=0;
}
pixel(int x,int y)
{
a=x;
b=y;
}
int geta()const //Accessor Function
{
a++; //error
return a;
}
void seta(int a1) //(Mutator Function)
{ a=a1;}
void setb(int b1) //(Mutator Function)
{
b=b1;
}
void display() const;
~pixel()
{
cout<<endl<<"destructor called";
}
};
void pixel::display() const
{
cout<<endl<<"a is="<<a<<endl<<"b is="<<b;
//a++;-error because display is constant function
}
Eg:-
const int func() // const return type
{
int a=10;
//a++; //error
return 4;
}
void main()
{
int a;
a = func();
cout<<endl<<"value of a is="<<a; //4
getch();
}
Program 233
//233.Example of Const argument in function
#include<iostream.h>
#include<conio.h>
class pixel
{
private:
int a;
int b;
public:
pixel()
{
a=0;
b=0;
}
pixel(int x,int y)
{
a=x;
b=y;
}
void store(const int v) //v=13
4|Page 22-12-2020
C++ Programming
{
a=v;
b=v;
// v++; cannot modify...
}
void display() const
{
cout<<endl<<"a is="<<a<<endl<<"b is="<<b;
}
~pixel()
{
cout<<endl<<"Destructor called";
}
};
void main()
{
clrscr();
pixel p2; a=13
p2.store(13); b=13
cout<<endl<<"Details of p2 object=";
p2.display();
getch(); p2 object
}
/*Output
Details of p2 are=
a is=13
b is=13 */
Const Object
1) An object of a class may be declared to be const, just like any other C++ variable.
2) For example:const pixel p1(70, 30);
3) The const property of an object goes into effect after the constructor finishes executing and
ends before the class's destructor executes.
4) We cannot modify the data members of a const object.
Program 234
//234.Const Object
#include<iostream.h>
#include<conio.h>
class Num
{
public:
int a;
void show()
{
cout<<endl<<"Value of a is="<<a;
}
void accept()
{
cout<<endl<<"Enter value of a is=";
cin>>a;
}
5|Page 22-12-2020
C++ Programming
void setA(int x)
{
a=x;
}
int getA()
{
return a;
}
Num()
{
a=0;
}
Num(int c)
{
a=c;
}
}; a=12 34
void main()
{
const Num n1(12); n1 object
clrscr();
n1.show(); //12
n1.setA(34);
n1.show(); //34
// n1.a=10; error because we cannot change value of data memeber of const object
getch();
}
/*Output
Value of a is=12
Value of a is=34 */
Const Pointer
1) If we make a pointer const, we cannot change the pointer.
2) This means that the pointer will always point to the same address but we can change the value
of that address.
6|Page 22-12-2020