The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types. A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.
C++ Class Definitions
When you define a class, you define a blueprint for a data type.
This doesn't actually define any data, but it does define what the class name means,
that is, what an object of the class will consist of and what operations can be
performed on such an object.
A class definition starts with the keyword class followed
by the class name; and the class body, enclosed by a pair of curly braces. A
class definition must be followed either by a semicolon or a list of declarations.
For example, we define the Box data type using the keyword class as
follows:
class A{
public:
int a
};
The keyword public determines the access attributes of the
members of the class that follows it. A public member can be accessed from
outside the class anywhere within the scope of the class object. You can also
specify the members of a class as private or protected which we
will discuss in a sub-section.
Define C++ Objects
A class provides the blueprints for objects, so basically an
object is created from a class. We declare objects of a class with exactly the
same sort of declaration that we declare variables of basic types. Following
statements declare two objects of class Box:
A.obj;
A.obj1;
Accessing the Data Members
The public data members of objects of a class can be accessed
using the direct member access operator (.). Let us try the following example
to make the things clear:
#include <iostream.h>
#include<conio.h>
class A{
public:
int i;
};
Void main( ){
A obj;
cout<<"Enter value of i:";
cin>>obj.i;
cout<<"i="<<obj.i;
}
i=5
Class Access Modifiers
Data hiding is one of the important features of Object Oriented
Programming which allows preventing the functions of a program to access
directly the internal representation of a class type. The access
restriction to the class members is specified by the labeled public,
private, and protected sections within the class body. The keywords
public, private, and protected are called access specifiers.
A class can have multiple public, protected, or private labeled
sections. Each section remains in effect until either another section label or
the closing right brace of the class body is seen. The default access for
members and classes is private.
The public Members
A public member is accessible from anywhere outside the
class but within a program. You can set and get the value of public variables
without any member function.
The private Members
A private member variable or function cannot be accessed,
or even viewed from outside the class. Only the class and friend functions can
access private members.
The protected Members
A protected member variable or function is very similar to
a private member but it provided one additional benefit that they can be
accessed in child classes which are called derived classes.
Constructor & Destructor
A class constructor is a special member function of a class
that is executed whenever we create new objects of that class. A constructor
will have exact same name as the class and it does not have any
return type at all, not even void. Constructors can be very useful
for setting initial values for certain member variables.
Default constructor
Default constructor
do not take any parameters(no arguments) called default constructor. If a
default constructor is not provide by the programmer explicitly, then the
compiler provides a implicit default constructor. In that case the default
constructor values of the variable are ‘0’.
Example of default constructor
#include<iostream.h>
#include<conio.h>
class A{
int x,y;
public:
A(){
x=5;
y=10;
}
void display(){
cout<<x <<" "<<y<<endl;
}
};
void main(){
clrscr();
A obj;
obj.display();
getch();
}
Output
5 10
Parameterized Constructor
A constructor which has parameters is
called parameterized constructor.It is used to provide different values to
distinct objects.
Example of parameterized constructor
#include<iostream.h>
#include<conio.h>
class A{
int x,y;
public:
A(int i,int j){
x=i;
y=j;
}
void display(){
cout<<x <<" "<<y<<"\n";
}
};
void main(){
clrscr();
A obj(20,45);
obj.display();
getch();
}
Output
20 45
Copy constructor
The copy constructor is a constructor which creates an
object by initializing it with an object of the same class, which has been
created previously. The copy constructor is used to:
·
Initialize one object from another of
the same type.
·
Copy an object to pass it as an
argument to a function.
Copy an object to return it from a function.
Example of copy constructor
#include<iostream.h>
#include<conio.h>
class A{
int x,y;
public:
A(int i,int j){
x=5;
y=10;
}
void display(){
cout<<x <<" "<<y<<"\n";
}
};
void main(){
clrscr();
A obj(20,45);
obj.display();
getch();
}
Output
5 6
5 6
Dynamic constructor
Dynamic constructor is used to allocate the memory to the objects at
the run time. Memory is allocated at run time with the help of ‘new’ operator.
By using this constructor we can dynamic initialize the objects.
Example of dynamic constructor
#include<iostream.h>
#include<conio.h>
class A{
int *a;
int i,n;
public:
A(int size){
n=size;
a=new int[n];
}
void getInput();
void display();
};
void A::getInput(){
cout<<"Enter elements in array:";
for(i=0;i<n;i++){
cin>>a[i];
}
}
void A::display(){
cout<<"Elements in array:";
for(i=0;i<n;i++){
cout<<a[i]<<"\t";
}
};
void main(){
clrscr();
int n;
cout<<"Enter value of n:";
cin>>n;
A obj(n);
obj.getInput();
obj.display();
getch();
}
Output
Enter value of n:5
Enter elements in array:1
2
3
4
5
Elements in array:1 2 3 4 5
Destructor
A destructor is a special member function of a class that
is executed whenever an object of it's class goes out of scope or whenever the
delete expression is applied to a pointer to the object of that class.
A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters. Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc.
Example of destructor
#include<iostream.h>
#include<conio.h>
class A{
public:
A(){
cout<<"Constructor\n";
}
~A(){
cout<<"Destructor\n";
}
};
void main(){
clrscr();
A obj,obj1;{
A obj2,obj3;
}
{
A obj4;
}
getch();
}
Output
Constructor
Constructor
Constructor
Constructor
Destructor
Destructor
Constructor
Destructor
Swap reference
Swap the value one position to another
position.
Example of swap reference
#include<iostream.h>
#include<conio.h>
void swap(int &p,int &q){
p=p*q;
q=p-q;
p=p-q;
};
void main(){
int i=5,j=6;
clrscr();
swap(i,j);
cout<<i<<" "<<j;
getch();
}
Output
6 5
Call by value
In call by value , value being passed
to the function is locally stored by the function parameter in stack memory
location. If you change the value of function parameters,it is change the
current function only.
Example
of call by value
#include<iostream.h>
#include<conio.h>
void display(int i){
cout<<i<<endl;
i=20;
};
void main(){
int i=5;
clrscr();
cout<<i<<endl;
display(i);
cout<<i;
getch();
}
Output
5
5
5
Call by reference
This method copy the address of argument into function as argument changes made to the parameters affect. the argument because address is used to access the actual argument actual and formal argument will be created in same memory location.
Example of call by reference
#include<iostream.h>
#include<conio.h>
void main(){
int i=5;
int &j=i;
clrscr();
cout<<i<<" "<<j<<endl;
i=20;
cout<<i<<" "<<j;
getch();
}
Output
5 5
20 20
Call by address
When the calling function passes arguments to
the called function using the call-by-value method,the only way to return the
modified value of the argument to the caller is explicitly using the return
statement.
Example of call by address
#include<iostream.h>
#include<conio.h>
void display(int *p){
cout<<*p<<endl;
*p=20;
};
void main(){
int i=5;
clrscr();
cout<<i<<endl;
display(&i);
cout<<i;
getch();
}
Output
5
5
20
Explicit Calling & Implicit Calling
The explicit function specifier
controls unwanted implicit type conversion. It can only be used in declarations
of constructor within a class declaration of constructor. For example except
for the default constructor, the constructor in the following class are
conversion constructor.
Example
of explicit & implicit calling
#include<iostream.h>
#include<conio.h>
class A{
public:
A(){
cout<<"Constructor\n";
}
};
void main(){
clrscr();
A obj=A(); //explicit calling
A obj; //implicit calling
getch();
}
Output
Constructor
Change function
Change
public member to private
#include<iostream.h>
#include<conio.h>
class A{
int x;
public:
A(int i){
x=i;
}
void display(){
cout<<x<<endl;
}
int & change(){
return x;
}
};
void main(){
A obj(5);
clrscr();
obj.display();
obj.change()=3;
obj.display();
getch();
}
Output
5
3
Default arguments
A default argument is a value provided
in a function declaration that is automatically assigned by the compiler if the
caller of the function does not provide a value for the argument value.
Example
of default argument
#include<iostream.h>
#include<conio.h>
void display(int x=6,int y=3){
cout<<x<<" "<<y<<endl;
}
void main(){
clrscr();
display();
display(1);
display(4);
getch();
}
Output
6 3
1 3
4 3
Inline function
C++ inline function is powerful concept that is commonly
used with classes. If a function is inline, the compiler places a copy of the
code of that function at each point where the function is called at compile
time. Any change to an inline function could require all clients of the
function to be recompiled because compiler would need to replace all the code
once again otherwise it will continue with old functionality.
To inline a function, place the keyword inline before the function name and define the function before any calls are made to the function. The compiler can ignore the inline qualifier in case defined function is more than a line.
Example
of inline function
#include<iostream.h>
#include<conio.h>
class A{
public:
inline void display();
};
inline void A::display(){
cout<<"Hello";
}
void main(){
A obj;
clrscr();
obj.display();
getch();
}
Output
Hello
This pointer
Every objects in c++ has access to its own address through an important pointer called this pointer. The this pointer is an implicit parameter to all member functions. Therefor inside a member function this may be used to refer to the invoking object. Friend functions do not have a this pointer, because friends are not members of a class. Only member function have a this pointer.
Example of this pointer
#include<iostream.h>
#include<conio.h>
class A{
int x,y;
public:
A(int x,int y){
this->x=x;
this->y=y;
}
void display(){
cout<<this->x<<" "<<this->y<<endl;
}
};
void main(){
A obj(5,6);
clrscr();
obj.display();
getch();
}
Output
5 6
Friend Functions
A friend function of a class is defined outside that class' scope
but it has the right to access all private and protected members of the class.
Even though the prototypes for friend functions appear in the class definition,
friends are not member functions.
A friend can be a function, function template, or member function,
or a class or class template, in which case the entire class and all of its
members are friends.
To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend.
Example of friend function
#include<iostream.h>
#include<conio.h>
class A{
int x;
public:
A(int i){
x=i;
}
friend void display(A ob){
cout<<ob.x<<endl;
}
};
void main(){
A obj(10);
clrscr();
display(obj);
getch();
}
Output
10
Example of Same method friend in more than one class
#include<iostream.h>
#include<conio.h>
class B;
class A{
int x;
public:
A(int i){
x=i;
}friend void display(A,B);
};
class B{
int y;
public:
B(int i){
y=i;
}friend void display(A,B);
};
void display(A ob,B ob1){
cout<<ob.x<<" "<<ob1.y<<endl;
}
void main(){
A obj(10);
B obj1(20);
clrscr();
display(obj,obj1);
getch();
}
Output
10 20
Example of method member of a class friend in another class
#include<iostream.h>
#include<conio.h>
class B;
class A{
int x;
public:
A(int i){
x=i;
}
void display(B);
};
class B{
int y;
public:
B(int i){
y=i;
}
friend void A::display(B);
};
void A::display(B ob1){
cout<<x<<" "<<ob1.y<<endl;
}
void main(){
A obj(10);
B obj1(20);
clrscr();
obj.display(obj1);
getch();
}
Output
10 20
Example of Unary pre-increment (++) operator overloading using friend function
#include<iostream.h>
#include<conio.h>
class A{
int x;
int y;
public:
A(int i , int j){
x=i;
y=j;
}
void display(){
cout<<x<<" "<<y<<endl;
}
friend void operator ++(A &ob){
++ob.x;
++ob.y;
}
};
void main(){
A obj(5,6);
clrscr();
cout<<"Before operator overloading=";
obj.display();
++obj;
cout<<"After operator overlaoding=";
obj.display();
getch();
}
Output
Before operator overloading=5 6
After operator overloading=6 7
Example of Unary post-increment (++) operator overloading using friend function
#include<iostream.h>
#include<conio.h>
class A{
int x;
int y;
public:
A(int i , int j){
x=i;
y=j;
}
void display(){
cout<<x<<" "<<y<<endl;
}
friend void operator ++(A &ob,int){
ob.x++;
ob.y++;
}
};
void main(){
A obj(5,6);
clrscr();
cout<<"Before operator overloading=";
obj.display();
obj++;
cout<<"After operator overlaoding=";
obj.display();
getch();
}
Output
Before operator overloading = 5 6
After operator overloading = 6 7
Example of Unary Operator overloading (-) using friend function
#include<iostream.h>
#include<conio.h>
class A{
int x;
int y;
public:
A(int i , int j){
x=i;
y=j;
}
void display(){
cout<<x<<" "<<y<<endl;
}
friend void operator -(A &ob){
ob.x=-ob.x;
ob.y=-ob.y;
}
};
void main(){
A obj(5,6);
clrscr();
cout<<"Before operator overloading=";
obj.display();-obj;cout<<"After operator overlaoding=";
obj.display();getch();
}
Output
Before operator overloading = 5 6
After operator overloading = -5 -6
#include<iostream.h>#include<conio.h>
class A{
int x;
int y;
public:
A(){}
A(int i , int j){
x=i;
y=j;
}
void display(){
cout<<x<<" "<<y<<endl;
}
friend A operator +(A ob,int p){
A t;
t.x=ob.x+p;
t.y=ob.y+p;
return t;
}
};
void main(){
A obj(5,6);
A obj1;
clrscr();
cout<<"Before operator overloading=";
obj.display();
obj1=obj+6;
cout<<"After operator overlaoding=";
obj1.display();
getch();
}
Output
Before operator overloading = 5 6
After operator overloading = 11 12
Static Function Members
By declaring a function member as static, you make it independent
of any particular object of the class. A static member function can be called
even if no objects of the class exist and the static functions are
accessed using only the class name and the scope resolution operator ::.
A static member function can only access static data member, other
static member functions and any other functions from outside the class.
Static Non-Static
Static Non-Static Static Non-Static
#include<iostream.h>
#include<conio.h>
class A{
public:static void display(){cout<<"Hello";
}
};
void main(){
clrscr();
A::display();
getch();
}
Output
Hello
Static Data Members of a Class
We can define class members static using static keyword.
When we declare a member of a class as static it means no matter how many
objects of the class are created, there is only one copy of the static member.
A static member is shared by all objects of the class. All static
data is initialized to zero when the first object is created, if no other
initialization is present. We can't put it in the class definition but it can
be initialized outside the class as done in the following example by
redeclaring the static variable, using the scope
resolution operator :: to identify.
Example of static data member
#include<iostream.h>
#include<conio.h>
class A{
public:
static int i;
static void display(){cout<<i++<<endl;}
};
int A::i=10;void main(){A obj,obj1;
clrscr();
A::display();
obj.display();
obj1.display();
cout<<A::i<<endl;
A::i=20;
obj.display();
getch();
}
Output
10
11
12
13
20
0 Comments