Vous êtes sur la page 1sur 29

1

C++ CLASS & OBJECT Ritesh jha

Class
It is a user defined data type.It provides a way by which we can bind
the data and its associated function together.It is a fundamental
building block of the object oriented programming language.It
implements all the concepts of OOPs. and ties them together.

In c++, a class makes a data type that is used to


create objects of the same type.

Object:-
It is an instance of the class with the same properties of that
class.

Before creating an object of a class we have to


specify that class.A class specification consist of two parts:-
Class declaration
Class method definition

Class declaration :-
It is a process to declare a class.To declare a class we
have to use the keyword class & we have to also give the name of
the class,member function of the class, accessibility of the members
of that class.These are the five basic elements which are required to
declare a class :-

Class tag

Javariteshjha11@rediffmail.com
2

C++ CLASS & OBJECT Ritesh jha

Data member
Member function
Access specifier
Object of the class

Class tag :-
It is the name of the class given by the programmer.

Data members :-
These are the attributes or variables of the class that describe
the characteristics of that class.Data members are optional in
a class declaration.

Member functions
These are used to define the functionality of the class.These are
also optional.

Access specifier :-
It defines the accessibility labels of the member of the class.It
may be public,private or protected.

Private :-
The private member of a class are only accessible from other
member functions of that class or its friend class or function.To
declare private class member we have to use the keyword private
followed by a colon(: ).
Javariteshjha11@rediffmail.com
3

C++ CLASS & OBJECT Ritesh jha

Protected :-
The protected members of a class is accessible from the
member function of the class and iss friend class and also from their
derived class. To declare protected class member we have to use the
keyword protected followed by a colon(: ).

Public :-
The public members of the class are accessible from anywhere
from which the class is visible.To declare public class member we have
to use the keyword public followed by a colon(: ).

Object of the class :-


It is the instance of the class with same properties as that of the
class.It is also optional.It may be consists of one or more valid object
identifier.

The general form of class declaration is :-


class class_tag
{
Access_specifier label1:
--------
---------- Data member
---------- & member function
Access_specifier label2:
--------
---------- Data member
---------- & member function
Javariteshjha11@rediffmail.com
4

C++ CLASS & OBJECT Ritesh jha

Access_specifier label3:
--------
---------- Data member
---------- & member function
}object_name;
Example:-
class sample
{
private:
--------------
--------------//data member &member function
--------------
public:
------------
------------ // data member &member function
-------------
Protected:
-------------
------------- // data member &member function
----------------
} s1, s2;

Accessing the member of a class


The private data of a class can be accessed only through the
member function of that class.
The main() can not access the private member of a class directly.
The public members of a class can be access by using dot(.)
operator.

Javariteshjha11@rediffmail.com
5

C++ CLASS & OBJECT Ritesh jha

The syntax for accessing the public member function of a


class is :-

Object_name.function_name(argument);

Example:-

S1.set_value(12,5);
The syntax for accessing the public data members of a class is :-

Object_name.data_member_name=value

Example :-

S1.a=20;

A data member declared in the public part of a class can be


accessed by the object using dot(.) operator.
The declaration of data member in the public sections of the
class violets the concept of data hiding and hence it should be
avoided.

Class method definition :-


The class method definition is also known as class function
definition.It is a set of statements which defines the functionality of
the member function of the class.There are two ways to defining the
body of a member function :-

1) Outside the class definition


2) Inside the class definition

Javariteshjha11@rediffmail.com
6

C++ CLASS & OBJECT Ritesh jha

Class method definition outside of the class definition


A member function definition outside of the class
definition is same as that of normal function definition.The only
difference is that the name of the function is fully qualified name.

The fully qualified name of a member function is


written as:

Class_name::member_function_name()

Where , class name is the name of the class in which the


function is exists & function name is the name of the function and
scope resolution operator(:: )is used to declare the body of the
function outside of the class.

The general form of a member function definition outside


of the class is :-
Return_type class_name::function_name(parameter)
{
----------------------
--------------------//body of the function
---------------------
}
Q:-WAP to accept a number and display it to illustrate the class
method definition outside of the class definition.
Ans :-
#include<iostream.h>
#include<conio.h>
class number
{
Javariteshjha11@rediffmail.com
7

C++ CLASS & OBJECT Ritesh jha

private:
int n;
public:
void get_val();
void put_val();
};
void main()
{
number n1;
clrscr();
n1.get_val();
n1.put_val();
getch();
}
void number::get_val()
{
cout<<Enter a number;
cin>>n;
}
void number::put_val()
{
Cout<<\n The given value is<<n;
}
Q:-WAP to accept the elements of a matrix and display them.
Ans :-
#include<iostream.h>
#include<conio.h>
class matrix
{
int m,n,r,c;

Javariteshjha11@rediffmail.com
8

C++ CLASS & OBJECT Ritesh jha

int m[10][10];
public:
void read_mat();
void display_mat();
};
void matrix::read_mat()
{
cout<<\n No. of row and column;
cin>>m>>n;
cout<<\n Enter the elements of the matrix \n;
for(r=0;r<m;r++)
{
for(c=0;c<n;c++)
cin>>m[r][c];
}
}
void display_mat()
{
cout<<\n The elements of given matrix is \n;
for(r=0;r<m;r++)
{
for(c=0;c<n;c++)
{
Cout<<m*r+*c+<<\t;
}
Cout<<\n;
}
}
void main()
{

Javariteshjha11@rediffmail.com
9

C++ CLASS & OBJECT Ritesh jha

matrix x;
clrscr();
x.read_mat()
x.display_mat();
getch();
}

Class method definition inside the class definition :-


When a member function is defined inside a class
definition then the member function definition is same as normal
function definition .We does not have to use the fully qualified name
of the function.A function declared inside a class definition is
automatically considered as inline.

Q:- WAP to accept a number & display them to demonstrate the use
of class method definition inside the class definition.
Ans:-
#include<iostream.h>
#include<conio.h>
class number
{
private:
int n;
public:
void get_val()
{
cout<<Enter a number;
cin>>n;
}
void put_val()
Javariteshjha11@rediffmail.com
10

C++ CLASS & OBJECT Ritesh jha

{
Cout<<\n The given value is<<n;
}
};
void main()
{
number n1;
clrscr();
n1.get_val();
n1.put_val();
getch();
}
Assignment 1
WAP accept a number and find their factorial.to demonstrate
the use of member function definition inside a class definition.
WAP accept number and check it is prime or composite.
WAP to display the multiplication tables between 2 & 10.
WAP to accept a number & find their reverse.
WAP to accept the elements of a matrix and find their transpose.

Array within a class :-


Array can be used as a data member of a class.It may be
declared in public,private or protected section of the class according
to our requirements.
Example:-
class myarray
{
int a[20];
public:

Javariteshjha11@rediffmail.com
11

C++ CLASS & OBJECT Ritesh jha

void getval();
void display();
};

Friend function:-
It is a special types of a function which is not a
member function of a class but access all the private
and protected members of the class like other member
function.
Following are The Characteristics of Friend Function :-
A friend function is not in the scope of the class to
which it has been declared as a friend.
Since it is not in the scope of the class ,so a friend
function can not be called using the object of the
class.It can be invoked like a normal function
without the help of any object.
A friend function can not access the member of the
class directly.It has to use object name and the
dot(.) membership operator with each member
name.

Javariteshjha11@rediffmail.com
12

C++ CLASS & OBJECT Ritesh jha

It can not be declared either in the public or private


part of a class without affecting its meaning.
It has objects as arguments.

Q:- WAP to find the sum of two numbers using friend


function.
Ans:-
#include<iostream.h>
#include<conio.h>
Class sot
{
Private:
Int a,b;
Public:
Void input();
Friend int sum(sot ob1);
};
Int sum(sot ob1)
{
Int R;
R=ob1.a+ob1.b;
Return R;
}
Void sot::input()
{
Cout<<Enter two numbers;
Javariteshjha11@rediffmail.com
13

C++ CLASS & OBJECT Ritesh jha

Cin>>a>>b;
}
Void main()
{
Sot ob;
Int r;
Clrscr();
Ob.input();
R=sum(ob);
Cout<<The required sum is<<R;
Getch();
}

Member function of One class as a friend of


Another class:
The member function of one class may be a friend of another
class.In such situations member functions are defined using
the scope resolution operator as shown below:

Class xyz
{
----------
----------
----------
Int add(); //Member function of class xyz
};
Class mno
{
Javariteshjha11@rediffmail.com
14

C++ CLASS & OBJECT Ritesh jha

----------
----------
----------
Friend int xyz::add(); //add() of class xyz be a friend of class mno
};

Notes :
A class can also be declared as a friend of another class.such a
class is known as a friend of another class.
A friend function acts as a bridge between more then one
classes.

Friend function as a Bridge :


If it is required for a function to operate on objects of more
then classes simultaneously ,then the same function can be
declared as a friend of all the classess.This is done by taking
the objects of all the classes as arguments so that the function
can operate on their private data.

Q:- WAP to accept two numbers in two different classes and display
the maximum number form them using friend function.
Ans:
#include<iostream.h>
#include<conio.h>
Class Second; //Forward declaration
Class First
{
Private:
Int a;
Javariteshjha11@rediffmail.com
15

C++ CLASS & OBJECT Ritesh jha

Public:
Void finput();
Friend void max(First f,Second s);
};
Class Second
{
Private:
Int b;
Public:
Void sinput();
Friend void max(First f,Second s);
};
Void max(First f,Second s)
{
Int M;
If(f.a>s.b)
M=f.a;
Else
M=s.b;
Cout<<\n the maximum number from them is<<M;
}
Void First::finput()
{
Cout<<Enter a number;
Cin>>a;
}
Void Second::sinput()
{
Cout<<Enter a number;
Cin>>b;

Javariteshjha11@rediffmail.com
16

C++ CLASS & OBJECT Ritesh jha

Void main()
{
First F1;
Second S1;
Clrscr();
F1.finput();
S1.sinput();
Max(F1,S1);
Getch();
}
Inline Function :-
It is a special type of function that accelerate the
execution of the program.The coding of a normal function and
an inline function is same except that an inline function
definition starts with a keyword inline.The compilation
process of inline function is totally different from normal
function.
In an inline function the function call statement is
replaced with the function code by the compiler then entire
code is compiled .The compiler does not have to jump from
one location to another location to execute the function and
then jump back since the code is already avilable to the calling
program.
Example:-
Class item
{
Javariteshjha11@rediffmail.com
17

C++ CLASS & OBJECT Ritesh jha

Int number;
Float cost;
Public :
Void getdata(int a,float b);
};
Inline void item::getdata(int a,float b)
{
Number=a;
Cost=b;
}
Nesting of member functions:-
When a member function call another member function of
that class then it known as nesting of member function.
Constant Member Function:-
If a member function of a class does not modify any data in
the class then this member function may be declared as a
constant member function using the keyword const.
Example:-
Int max(int,int) const;
Void xyz(void) const;

Static data member


Static data members are used to maintain common values to
all the object of the class.The type and scope of each static
data member must be defined outside of the class definition.
The general form of declaring a ststic datab member is :-

Javariteshjha11@rediffmail.com
18

C++ CLASS & OBJECT Ritesh jha

Type class_name::static_member_name;

Following are the characteristics of static data members:-


It is initialized to be zero when the first object of the class
is created.No other initialization is allowed.
Only one copy of the data member is created for entire
class and is shared by all the objects of that class.
It is independent of number of objects created.
It is visible oly within the class but its scope is through
out the program.
Example:-
#include<iostream.h>
#include<conio.h>
Class person
{
Static int obcount;
Int n;
Public:
Void getdata(int a)
{
n=a;
obcount++;
}
Void getcount()
{
Cout<<Object count :- <<obcount;
}
Javariteshjha11@rediffmail.com
19

C++ CLASS & OBJECT Ritesh jha

};
Int person::obcount; //definition of static data member
void Main()
{
Person a,b,c;
Clrscr();
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
b.getdata(200);
c.getdata(300);
cout<<\n after reading data\n;
a.getcount();
b.getcount();
c.getcount();
getch();
}
Output:
Object count :- 0
Object count :- 0
Object count :- 0
After reading data :
Object count :- 3
Object count :- 3
Object count :- 3

Javariteshjha11@rediffmail.com
20

C++ CLASS & OBJECT Ritesh jha

Static member function:-


A static function can access only other static data members or
member functions delared in the class.
A static member function can be caleed using the class
name instead of its object name as follows:
Class_name::function_name;

Q): WAP to count the number of objects created at run time


using static data members and static member functions.
Ans:-
#include<iostream.h>
#include<conio.h>
Class item
{
Int code;
` static int obcount;
Public:
Void getcode()
{
Code=++obcount;
}
Void putcode()
{
Cout<<Object number<<code<<\n;
}
Static void putcount()
{
Javariteshjha11@rediffmail.com
21

C++ CLASS & OBJECT Ritesh jha

Cout<<Count:<<obcount<<\n;
}
};
Int item::obcount;
Void main()
{
Item it1,it2;
Clrscr();
it1.getcode();
it2.getcode();
item::putcount(); //calling static member function
item it3;
it3.getcode();
item::putcount();
it1.putcode();
it2.putcode();
it3.putcode();
getch();
}
Output:-
Count : 2
Count : 3
Object number :1
Object number : 2
Object number : 3

Note :-
Javariteshjha11@rediffmail.com
22

C++ CLASS & OBJECT Ritesh jha

Scope is the area of a program in which a variable or


function is accessible.
Class within a class is known as nested class .It is also
known as Containership.

Array of object
Q): WAP to accept the information of 20 workers
and display them using class.
Ans:-
#include<iostream.h>
#include<conio.h>
Class worker
{
Char name[20];
Float age;
Public:
Void getdata();
Void putdata();
};
Void worker::getdata()
{
Cout<<Enter name;
Cin>>name;

Javariteshjha11@rediffmail.com
23

C++ CLASS & OBJECT Ritesh jha

Cout<<Enter the age;


Cin>>age;
}
Void worker::putdata()
{
Cout<<Name=<<name;
Cout<<\nAge=<<age<<Years\n;
}
Void main()
{
Worker Ob[30];
Int i;
Clrscr();
Cout<<Enter the information of 30 Emp;
For(i=0;i<30;i++)
{
Cout<<Enter the information
of<<i+1<<Emp;
Ob[i].getdata();
}
Cout<<\n;
For(i=0;i<30;i++)
{

Javariteshjha11@rediffmail.com
24

C++ CLASS & OBJECT Ritesh jha

` cout<<The information of
<<i+1<<Emp;
Ob[i].putdata();
Cout<<\n#####################\n;
}
Getch();
}

Structure within a class


Q:-WAP to demonstrate the use of a structor within a class.
Ans:-
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct customer
{
char name[20],atype[5];
int anum;
float bamt;
};
class Bank_account
{
customer c_list[10];
public:
void initial_data();

Javariteshjha11@rediffmail.com
25

C++ CLASS & OBJECT Ritesh jha

void dep_money(float d_amt,int an);


void wd_money(float wd_amt,int an);
void display_data(int an);
};
void Bank_account::initial_data()
{
int i,f=0;
for(i=0;i<10;i++)
{
cout<<\nEnter the data of customer :<<i+1;
cout<\n Name : ;
gets(c_list[i].name);
fflush(stdin);
cout<<\n account type : ;
cin>>c_list[i].atype;
cout<<\n Account number : ;
cin>>c_list[i].anum;
cout<<\n Enter the bvalance amount\n;
cin>>c_list[i].bamt;
}
}
void Bank_account::dep_money(float dbal,int an)
{
int i,f=0;
for(i=0;i<10;i++)
{
If(c_list[i].anum==an)
Javariteshjha11@rediffmail.com
26

C++ CLASS & OBJECT Ritesh jha

{
c_list[i].bamt+=dbal;
f=1;
break;
}
}
If(!f)
Cout<<\n Not a valid account number .;
}
void Bank_account::wd_money(float wamt,int an)
{
int i,f=0;
for(i=0;i<10;i++)
{
If(c_list[i].anum==an)
{
If(c_list[i].bamt<1000&&c_list[i].bamt-1000<bamt)
{
c_list[i].bamt-=wamt;
f=1;
break;
}
else
cout<<ur balance amt is minimum . u cant;
}
}
If(!f)
Javariteshjha11@rediffmail.com
27

C++ CLASS & OBJECT Ritesh jha

cout<<\n Not a valid account number .;


}
void Bank_account::display_data(int an)
{
int i,f=0;
for(i=0;i<10;i++)
{
If(c_list[i].anum==an)
{
cout<<\n The information of customer <<i+1<<is\n;
cout<\n Name : ;
puts(c_list[i].name);fflush(stout);
cout<<\n account type : <<c_list*i+.atype;
cout<<\n Account number : <<c_list[i].anum;
cout<<\n Balance amount\n<<c_list[i].bamt;
f=1;
break;
}
}
If(!f)
Cout<<\n Not a valid account number .;
}
void main()
{
int ch,amt,acno;
clrscr();
Bank_account b;
Javariteshjha11@rediffmail.com
28

C++ CLASS & OBJECT Ritesh jha

b.initial_data();
do
{
cout<<\n Menu options \n;
cout<<\n 1 for deposite money \n;
cout<<\n 2 for deposite money \n;
cout<<\n 3 for display the records \n;
cout<<\n 4 for exit \n;
cout<<\n Enter ur choice;
cin>>ch;
switch(ch)
{
case 1:
cout<<Enter the amount to deposite;
cin>>amt;
cout<<Enter the account number;
cin>>acno;
b.dep_money(amt,acno);
break;
case 2:
cout<<Enter the amount to widthdraw;
cin>>amt;
cout<<Enter the account number;
cin>>acno;
b.wd_money(amt,acno);
break;
case 3:
Javariteshjha11@rediffmail.com
29

C++ CLASS & OBJECT Ritesh jha

cout<<Enter the account number to display records;


cin>>acno;
b.display_data(acno);
break;
case 4:
exit(0);
default :
cout<<\n In valid ur choice.;
}
}while(ch!=4);
getch();
}

Javariteshjha11@rediffmail.com

Vous aimerez peut-être aussi