Class Object
Class Object
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.
Object:-
It is an instance of the class with the same properties of that
class.
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
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(: ).
[email protected]
3
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(: ).
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;
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;
Class_name::member_function_name()
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;
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()
{
matrix x;
clrscr();
x.read_mat()
x.display_mat();
getch();
}
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()
[email protected]
10
{
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.
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.
Cin>>a>>b;
}
Void main()
{
Sot ob;
Int r;
Clrscr();
Ob.input();
R=sum(ob);
Cout<<The required sum is<<R;
Getch();
}
Class xyz
{
----------
----------
----------
Int add(); //Member function of class xyz
};
Class mno
{
[email protected]
14
----------
----------
----------
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.
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;
[email protected]
15
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;
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
{
[email protected]
17
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;
Type class_name::static_member_name;
};
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
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 :-
[email protected]
22
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;
` cout<<The information of
<<i+1<<Emp;
Ob[i].putdata();
Cout<<\n#####################\n;
}
Getch();
}
{
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)
[email protected]
27
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:
[email protected]
29