03ClassAndObject
03ClassAndObject
Unit 2:
LECTURE-13
CLASS:-
Class is a group of objects that share common properties and relationships .In C++, a class is
a new data type that contains member variables and member functions that operates on the variables.
A class is defined with the keyword class. It allows the data to be hidden, if necessary from external
use. When we defining a class, we are creating a new abstract data type that can be treated like any
other built in data type.
Generally a class specification has two parts:-
a) Class declaration
b) Class function definition
the class declaration describes the type and scope of its members. The class function
definition describes how the class functions are implemented.
Syntax:-
class class-name
{
private:
variable declarations;
function declaration ;
public:
variable declarations;
function declaration;
};
The members that have been declared as private can be accessed only
from with in the class. On the other hand , public members can be accessed from outside the class
also. The data hiding is the key feature of oops. The use of keywords private is optional by default,
the members of a class are private.
The variables declared inside the class are known as data members and the functions
are known as members mid the functions. Only the member functions can have access to the private
data members and private functions. However, the public members can be accessed from the outside
the class. The binding of data and functions together into a single class type variable is referred to as
encapsulation.
Syntax:-
class item
{
int member;
float cost;
public:
void getldata (int a ,float b);
void putdata (void);
The class item contains two data members and two function members, the data
members are private by default while both the functions are public by declaration. The function
getdata() can be used to assign values to the member variables member and cost, and putdata() for
displaying their values . These functions provide the only access to the data members from outside
the class.
CREATING OBJECTS:
Once a class has been declared we can create variables of that type
by using the class name.
Example:
item x;
creates a variables x of type item. In C++, the class variables are known as objects. Therefore
x is called an object of type item.
LECTURE-14
Syntax:
Example:
void item :: getdata (int a , float b )
{
number=a;
cost=b;
}
void item :: putdata ( void)
{
cout number=: number<<endl;
cout cost= cost<<endl;
}
The member function have some special characteristics that are often used in the program
development.
everal different classes can use the same function name. The "membership label"
will resolve their scope, member functions can access the private data of the class
.A non member function can't do so.
A member function can call another member function directly, without using the dot
operator.
Output: object x
number 100
cost=299.950012
object -4
cost=175.5
Q.
Write a simple program using class in C++ to input subject mark and prints it.
ans:
class marks
{
private :
int ml,m2;
public:
void getdata();
void displaydata();
};
void marks: :getdata()
{
cout enter 1st subject mark: ;
cin>>ml;
cout enter 2nd subject mark: ;
cin>>m2;
}
void marks: :displaydata()
{
cout Ist subject mark: ml<<endl ;
cout 2nd subject mark: m2;
}
void main()
{
clrscr();
marks x;
x.getdata();
x.displaydata();
A member function can be called by using its name inside another member function of the
same class. This is known as nesting of member functions.
#include <iostream.h>
class set
{
int m,n;
public:
void input(void);
void display (void);
void largest(void);
};
int set::largest (void)
{
if(m>n)
return m;
else
return n;
}
void set::input(void)
{
cout input values of m and n: ;
cin>>m>>n;
}
void set::display(void)
{
cout largestvalue= largest() \n ;
}
void main()
{
set A;
A.input( );
A.display( );
}
output:
3017
largest value= 30
Although it is a normal practice to place all the data items in a private section and all the functions in
public, some situations may require contain functions to be hidden from the outside calls. Tasks such
as deleting an account in a customer file or providing increment to and employee are events of
serious consequences and therefore the functions handling such tasks should have restricted access.
We can place these functions in the private section.
A private member function can only be called by another function that is a member of its class. Even
an object can not invoke a private function using the dot operator.
Class sample
{
int m;
void read (void);
void write (void);
};
if si is an object of sample, then
s.read();
is illegal. How ever the function read() can be called by the function update ( ) to
update the value of m.
void sample :: update(void)
{
read( );
}
#include<iostream.h>
class part {
private:
int modelnum,partnum;
float cost;
public:
void setpart ( int mn, int pn ,float c) {
modelmim=mn;
partnum=pn;
cost=e;
}
output:- model:644
num:73
cost: $217550003
model: 567
num:89
cost: $759.549988
LECTURE-16
#include<iostream.h>
#include<conio.h>
class employee
{
private:
char name[20];
int age,sal;
public:
void getdata();
void putdata();
};
void employee : : getdata ()
{
cout enter name : ;
cin>>name;
cout enter age : ;
cin>>age;
cout enter salary:
cin>>sal;
return(0);
}
void employee : : putdata ( )
{
cout<<name <<endl;
cout<<age<<endl;
cout<<sal<<endl;
return(0);
}
int main()
{
employee emp[5]:
for( int i=0;i<5;i++)
{
emp[i].getdata();
}
cout<<endl;
for(i=0;i<5;i++)
{
emp[i].putdata();
}
getch();
return(0);
}
ARRAY OF OBJECTS:-
#include<iostream.h>
#include<conio.h>
class emp
{
private:
char name[20];
int age,sal;
cin>>sal;
}
void emp :: putdata ()
{
cout << emp name:
cout emp age:”<< name<<endl<<age<<endl;
cout emp salary: sal;
}
void main()
{
emp foreman[5];
emp engineer[5];
for(int i=0;i<5;i ++)
{
cout for foreman: ;
foreman[i] . getdata();
}
cout<<endl;
for(i=0;i<5;i++)
{
Foreman[i].putdata(); .
}
void sort ::
putdata() {
int k;
for(k=0;k<10;k+
+) {
cout<<num [k] <<endl ;
}
}
int
main() {
clrscr();
sort s;
s.getdata(
);
s.putdata(
);
return(0);
}
ARRAY OF MEMBERS:
#include<iostream.h>
#include<constream.h>
const int m=50;
class items
{
int item_code[m];
float item_price[m];
int count;
public:
void cnt(void) { count=0;}
void get_item(void);
void display_sum(void);
void remove(void);
void display _item(void);
sum=sum+item_price[i];
}
cout<< \n total value: sum<<endl;
}
int main ( )
{
items order;
order.cnt();
int x;
do
{
cout \nyou can do the following: ;
cout enter appropriate no: ;
cout<<endl ;
cout<<endl 2: display total value : ;
cout<<endl 3 : display an item ;
cout<<endl 4 :display all item: ;
cout<<endl 5 : quit: ;
cout<<endl<<endl what is your option: ;
cin>>x;
switch(x)
{
case 1: order.get_item(); break;
case 2: order.display_sum(); break;
cose 3: order.remove(); break;
case 4: order.display_item();break;
case 5: break;
default : cout error in input; try again ;
}
} while(x!=5);
}
LECTURE-17
Note that the type and scope of each static member variable must be defined
outside the class definition .This is necessary because the static data members are stored
separately rather than as a part of an object.
Example :-
#include<iostream.h
> class
item
{
static int count; //count is
static int number;
public:
void getdata(int
a) . {
number=
a;
count++;
}
void
getcount(void) {
cout count: ;
cout<<count<<end
l;
}
};
int item :: count ; //count
defined int
main( )
{
item a,b,c;
a.get_count(
);
b.get_count(
);
c.get_count(
): a.getdata(
): b.getdata(
);
c.getdata( );
cout«"after reading data :
"«endl; a.get_count( );
b.gel_count( );
c.get count(
); return(0);
}
The static Variable count is initialized to Zero when the objects created . The count
is incremented whenever the data is read into an object. Since the data is read into objects three
times the variable count is incremented three times. Because there is only one copy of count
shared by all the three object, all the three output statements cause the value 3 to be displayed.
main()
{
test t1,t2;
t1.setcode(
);
t2.setcode(
);
output:- count :
2 count:
3
object number
1 object
number 2
object number
3
Like any other data type, an object may be used as A function argument. This can cone in two
ways 1. A copy of the entire object is passed to the function.
2. Only the address of the object is transferred to the function
The first method is called pass-by-value. Since a copy of the object is passed to the function,
any change made to the object inside the function do not effect the object used to call the function.
The second method is called pass-by-reference . When an address of the object is passed, the called
function works directly on the actual object used in the call. This means that any changes made to
the object inside the functions will reflect in the actual object .The pass by reference method is
more efficient since it requires to pass only the address of the object and not the entire object.
Example:-
#include<iostream.h
> class time
{
int hours;
int
minutes;
public:
void gettime(int h, int
m) {
hours=h;
minutes=
m;
}
void
puttime(v
oid) {
cout<< hours hours and: ;
cout<<minutes minutes:
end; }
int main()
{
time T1,T2,T3;
T1.gettime(2,45);
T2.gettime(3,30);
T3.sum(T1,T2);
cout T1= ;
T1.puttime( );
cout T2= ;
T2.puttime( );
cout T3= ;
T3.puttime( );
return(0);
}