CLASS
Lecture#2
1
 Class is the most important feature of C++ that supports object oriented programming
(OOP).
 In OOP, program is divided into objects.
 OOP is an easy and flexible approach for designing and organizing the program.
 A program is designed by using classes.
 A class is a collection of data(data members) and functions (member functions or
methods).
 The data items and functions are defined within the class.
2
 The functions are written to work upon the data items and each function has a unique
relationship with the data items of the class.
 Classes are defined to create user defined data types.
 Definition of the data types does not create any space in the computer memory.
 When a variable of that data type is declared, a memory space is reserved for that
variable. but when a class is defined, it does not occupy any space in the computer
memory.
 Class defines the data item and the member function that can be used to work upon its
data items.
 Defining a class only specifies its data members and the relationship between the data
items through its functions. 3
SYNTAX OF CLASS
Class Class_name
{
Body of the class
};
class class-name
{
private data and functions
access-specifier:
data and functions
} object-list; (optional)
4
Class : A keyword used to represents a class
Class_name: represents the name of the class
Body of Class: The body of a class consist of the data items and the
functions. These are called members of the class these are written
between braces.
Semicolon (;)
The body of a class end with semicolon. 5
#include <iostream.h>
// using namespace std;
class myclass
{
public:
int i, j, k; // accessible
};
int main()
{
myclass a, b;
a.i = 100;
a.j = 4;
a.k = a.i * a.j;
b.k = 12;
cout << a.k << " " << b.k;
return 0;
}
6
MEMBER OF A CLASS
 A class contains data items (variable) and functions. Theses are called member of the class. The
data item are data members and the function are called member functions.
 By default all functions and variables declared inside a class are private to that class.
 To declare public variable ,Keyword Public is used
 Data members
Class abc
{
int w, x, y, z;
float a, b;
};
Where a, b, w, x, y and z are the data members of the class “abc”.
7
MEMBER FUNCTIONS
 The function of the class that are defined to work on its data
members are called member functions of the class.
 Member functions can be defined within the class or outside.
8
// A Simple class declaration
Class myclass
{
//private to myclass
int a;
Public:
Void set_a (int num);
Int get_a();
};
 myclass contains one private variable ,a.
 Two public functions set_a( ) and get_a ( ).
 Function are declared within a class, are called member
functions.
 Variable a is private and cannot be accessible by any code
outside myclass.
9
Class xyz
{
private:
Int a, b, c;
Public:
Void get (void)
{
Cout<<“enter value for a , b and c”;
Cin>>a>>b>>c;
}
Void put (void)
{
Cout<<“a = ”<<a<<endl;
Cout<<“b= ”<<b<<endl;
Cout<<“c = ”<<c<<endl;
}
};
In this class
  Three data members
◦ (a, b, & c)
 Two member functions
◦ (“get” , ”put”)
“get” function is used to input values
into data members a , b ,& c.
“Put” function is used to print values
of the data members. 10
MEMBER ACCESS SPECIFIERS
 The commands that determine whether a member of a class can be
accessed from outside the class or not are called member access
specifiers.
 There are three types of member access specifiers:
 Private
 Public
 Protected
11
PRIVATE SPECIFIERS:
• The members of a class that can be accessed only within the class are called
private members. Private specifiers cannot can accessed from outside that class.
• Normally all data members are declared as private.
• Members functions can also be declared as private.
• The default access mode is private. If no access specifiers is given . The
members are treated as private.
12
PUBLIC SPECIFIERS:
 Public members of the class can be accessed both inside and outside from the class.
 The member functions and data members both are declared as public.
 The protected access specifier is needed only when inheritance is involved.
13
Class cdate
{
Private:
Int y, d, m;
Public:
Void gdate(void)
{
Cout<<“enter year ?”;
Cin>>y;
Cout<<“enter month?”;
Cin>>m;
Cout<<“enter day?”;
Cin>>d;
Void pdate (void)
{
Cout<<d<<“/”<<m<<“/”<<y;
}
};
14
 In the above class:
 “gdate” and “pdate” are the two member functions used in class.
 Both member functions are declared public.
15
OBJECTS
 The variable or instances of a class are object.
 A class may contain several data items and functions.
 Combining of data and member functions into one unit called encapsulation.
 An object represents the data members of a class in the memory. Each object of a class has a
unique name.
 Function in an object are called the member functions. They are known as the methods.
 The member function are used to process and access data of a object.
16
DECLARING OBJECTS OF A CLASS
 The objects of a class are declared in the similar way as the variables of any data or
structure types are declared.
 While defining a class no space is reserved for it the memory but when an object of a
class is declared, a memory space is reserved for that objects.
 Syntax to create objects of a class:
Class_ name object_name;
e.g.
awkum cs, phy, chem;
17
CALLING MEMBER FUNCTIONS
 The member functions of a class is called through an object of the class.
 The DOT operator is used .
 The Dot operator Connects the Object name and the member function.
 The General syntax of Calling a member function is :
Object_name. member_function ( with argument in case of a function)
For example , if “add” is the name of the object and “Pdate” is the member function than the
meber function is called as shown below:
add.pdate ( );
Only those member functions can be accessed from outside the class with the dot operator that
have been declared as public 18
#include<iostream. h>
#include<conio. h>
Class edate
{
private:
int y, m, d;
public:
void gdate (void)
{
cout<<“Enter year ?”;
cin>>y;
cout<<“Enter
Month?”;
cin>>m;
cout<<“Enter Day?”;
cin>>d;
}
Void pdate (void)
{
cout<<“Date is: “<<endl;
cout<<d<<“/”m<<“/”<<y;
}
};
Void main()
{
edate aniv;
Clrscr ();
aniv.gdate ();
aniv.pdate();
getch();
}
edate= 3 data members
Y, n, d
2 member function
“gdate” & “pdate”
Output of program
Enter year? 1980
Enter Month ?1
Enter Day ? 27
Date is : 27/1/1980
19
WRITE A PROGRAM BY USING A CLASS TO INPUT TWO VALUES USING A MEMBER FUNCTION OF A CLASS .
DISPLAY THE SUM OF THE TWO VALUES BY USING ANOTHER MEMBER FUNCTION OF THE CLASS
#include<iostream. h>
Class sum
{
Private:
int n, m;
Pubic :
Void get (int a, int b)
{
n=a;
m=b;
}
Void display (void)
{
Cout << “sum=“ <<n + m;
}
};
Void main()
{
sum s;
int x, y;
Cout<<“Enter first number ?”;
Cin >> x ;
Cout <<“Enter second number ?”;
Cin >> y;
s. get (x, y);
s. display();
}
20
Write a program by using a class employee to input the record
of employees
 Define the following data members:
name, bpay, h_rent, gpay
 Define the following member functions:
input data in name & bpay
calculate h _rent , ma, gpay
print the complete record on the computer system
 h_rent= house rent =60% of bpay
ma= medical allowance =20% of bpay
gpay = bpay + h_rent +ma
21
#include<iostream. h>
#include <conio.h>
Class employee
{
Private:
Char name [15];
Float bpay. h_rent, ma, gpay;
Public:
Void input (void)
{
Cout<< “Enter name of Employee
?”;
Cin>>name;
Cout<<“Enter basic pay of
employee?”;
Cin>>bpay; }
Void allow (void)
{
h_rent = bpay *(60/100);
ma= bpay* (20/100);
gpay= bpay+ h_rent + ma;
}
Void display (void)
{
Clrscr ();
Cout<<“Name of
employee:”<<name<<endl;
Cout<<“Basic pay:”<<bpay<<endl;
Cout<<“House rent:”<<h_rent<<endl;
Cout<<“Medical allowance:”<<ma<<endl;
Cout<<“Net pay:”<<gpay<<endl;
}
};
Void main ( )
{
Employee obj1;
Clrscr( )
obj1.input ( );
obj1.allow( );
Obj1.display ( );
getch();
}
22
#include <iostream. h>
#include <conio.h>
Class student
{
Private :
Char name [15];
Float s1, s2, s3, total, avg;
Public:
Void get_data(void)
{
Cout<<“Enter name of the student ?”;
Cin>>name;
Cout<<“Enter marks of 1st subject?”;
Cin>>s1;
}
Write a class to input the name of student and marks of three subjects. Calculate the total marks
and average marks.
Cout<<“Enter marks of 2nd subject?”;
Cin>>s2;
Cout<<“Enter marks of 3rd subject?”;
Cin>>s3;
total = s1+ s2+ s3;
avg = total / 3;
}
Void show (void)
{
Clrscr ();
Cout<<“name of student :”<< name;
Cout<<endl;
Cout<<“marks of 1st subject:”<<s1;
Cout<<endl;
Cout<<“marks of 2nd and 3rd subject:”<< s2 <<
s3;
Cout<< “total
marks”<<total;
Cout<<endl;
Cout<< “average
marks”<<avg;
Cout<<endl;
} };
Void main ( )
{
Student abc;
abc.get _data( );
abc.show ( );
getch( );
}
23
DEFINING MEMBER FUNCTION OUTSIDE THE CLASS
 A member function of a class can also be defined outside the class.
 The prototype of the function is declared inside the class.
 The member function are defined outside the class in the similar way as user defined function are
defined.
 The Scope Resolution operator (: :) is used in member function declarator to define the function of
class outside the class
Syntax of member function definition outside the class:
type class_name : : function_name (arguments)
{
body of function
}
24
Type represents the data type of value returned by the member function
Class_name represents the class name to which the member function belong
: : double colon represents the scope resolution operator that is used
to define the body of the member function of the
specified class
Function_name represents the name of the member function of a class with
arguments (if any)
Body of the function the set of statements of the functions
The member function is defined outside of its class only if the body of the function
definition is large. Otherwise the function definition should be defined inside the class
25
WRITE A PROGRAM TO INPUT AND PRINT THE RECORD OF AN EMPLOYEE BY DEFINING
MEMBER FUNCTION OUTSIDE THE CLASS
#include <iostream. h>
#include <conio.h>
Class employee
{
Private :
Char name [15];
Float bpay , h_rent , ma gpay;
Public:
Void input (void);
Void allow (void);
Void display(void);
};
void main( )
{
Employee obj;
clrscr( );
obj. input ( );
obj. allow( );
26
Obj. display ( );
}
Void employee : : input(void)
{
Cout<<“enter name of employee? ”;
Cin>>name;
Cout<<“ enter basic pay of employee ?”;
Cin >> bpay;
}
Void employee : : allow (void)
{
h_rent = bpay * (60/100);
ma = bpay * ( 20/100);
gpay= bpay + h_rent + ma ;
}
Void employee : : display (void)
{
Clrscr ();
Cout<< “name of employee :”<<
name<<endl;
Cout<< “Basic pay :”<< bpay<<endl;
Cout<< “house rent:”<<h_rent<<endl;
Cout<< “medical allowance: ” <<
ma<<endl ;
Cout<< “Net pay:”<< gpay<<endl ;
}
STORAGE OF OBJECTS IN MEMORY
 When an object of a class is created, a memory is reserved in the computer
memory to hold its data members.
 Separate memory spaces are reserved for each class object.
 The member function of a class are stored at only one location in the
computer memory.
 All objects of the class use the same member functions to process data.
 Each object has separate memory space for data members.
 The member function of the class are stored in only one place and are shared
by all the objects of the class
27
#include<iostream. h>
#inlcude<conio. h>
Class temp
Private :
int x;
Float y;
Public :
Void getdata (void)
{
Cout << “ enter value of x =”;
Cin>> x;
Cout << “ enter value of y =”;
Cin>> y;
}
Void print (void)
{
Cout<<“enter value of X=”<<x<<endl;
Cout<< “Enter value of y=”<<y<<endl;
28
}
};
Void main( )
{
temp a, b;
Cout <<“get data for object a”<<endl;
a.getdata ( );
Cout <<“get data for object b”<<endl;
b. getdata ( );
Cout <<“Data in object a”<< endl;
a. Print ( );
Cout <<“Data in object b”<< endl;
b. Print ( );
getch( );
}
The storage of object a and b as mentioned in the above program example is
shown below. These object use the same number functions.
29
getdata()
Print ()
Object a Object b
x
y
x
y
CONSTRUCTOR
 A constructor is member function of a class that is called and executed
automatically when an object of that class is created.
 Automatic initialization using an objects can be performed through constructor.
 The name of the constructor function is same as the name of the class.
 Any initialization that need to be performed on an object can be done automatically
by the constructor function.
 A constructor function may have arguments but it cannot return any value.
 No return type
30
# include <iostream. h>
#include<conio.h>
Class test
{
Public :
test ( )
{
Cout<<“welcome”<<endl;
abc ( )
{
Cout<<“OK”<<endl;
};
31
Void main( )
{
test a, b, c;
}
Output of program
Welcome
welcome
welcome
#include <iostream. h>
#include <conio. h>
Class myclass
{
Public:
int a;
myclass ( );
Void show( );
};
myclass : : myclass( )
{
Cout<<“In constructorn”;
a=10;
}
32
Void myclass : :show ( )
{
Cout<<a;
}
int main( )
{
myclass ob;
ob. show ( );
return 0;
}
 The value of a is initialized by the constructor myclass( ).
 The constructor is called when the object ob is created.
 myclass( ) has no return type.(constructor does not return any value)
 For global objects, an object’s constructor is called once, when the
program first begins executions.
 For local objects, the constructor is called each time the declaration
statements is executed.
33
INITIALIZING DATA USING CONSTRUCTOR
 The constructor function is normally used to initialize values in data members
of a class when the program is executed.
 This type of initialization is called the automatic initialization.
 For example; If a member functions has two arguments of int type the
specified value can be assigned to the data member of class using a
constructor.
34
#include <iostream. h>
#include <conio. h>
Class sum
{
Private:
int n, m ,s;
Public:
sum( int x, int y)
{
n=x;
m=y;
s=n + m;
} 35
psum( )
{
Cout<<“sum of ”<<n<<“and”<<m
<<“=“<<s<<endl;
}
};
Void main( )
{
sum a(16,10), b(2, 3);
a. psum( );
b. psum( );
getch( );
}
Output
Sum of 16 and 10= 26
Sum of 2 and 3= 5
DESTRUCTOR
 The complement of a constructor is destructor.
 This function is called when an object is destroyed.
 An object allocates memory when it is created. But when the destructor
functions is called; the memory of the objects gets free.
 The destructor functions has the same name as the name of the a class but
tilde sign (~) is written before its name.
 Like constructor, Destructor also don't return any value.
36
#include<iostream.h>
#include<conio.h>
Class myclass
{
int a;
Public:
myclass( ); //constructor
~myclass ( ); // destructor
Void show( );
};
Myclass :: myclass ( )
{
Cout<<“In constructor n”;
a=10;
} 37
myclass :: ~myclass ( )
{
Cout<<“Destructing …n”;
}
Void myclass : : show ( )
{
Cout<<a<<“n”;
}
int main( )
{
myclass ob;
ob.show( );
return 0;
}
#include <iostream.h>
#include<conio.h>
Class program
{
pubic:
program( )
{
cout<<“constructor functions ”<<endl;
}
~program ( )
{
cout<<“destructor function”<< endl;
}
}; 38
Void main( )
{
Program x;
int a, b, s;
a=10;
b=20;
s= a + b;
Cout<<“the sum numbers is ”<< s
<<endl;
getch( );
}
Output
constructor function
Sum of two number is 30
destructor function
#include <iostream>
using namespace std;
class Counter
{
private:
int count; //count
public:
Counter( ) //constructor
{
count=0;
}
void inc_count( ) //increment count
{
count++;
}
int get_count( ) //return count
{ 39
return count;
}
};
int main( )
{
Counter c1, c2; //objects
cout << “nc1=” << c1.get_count( ); //display
cout << “nc2=” << c2.get_count( );
c1.inc_count( ); //increment c1
c2.inc_count( ); //increment c2
c2.inc_count( ); //increment c2
cout << “nc1=” << c1.get_count( ); //display again
cout << “nc2=” << c2.get_count( );
cout << endl;
return 0;
}
Robert Lafore BOOK 4th edition page :229
Output
c1=0
c2=0
c1=1
c2=2
CONSTRUCTOR WITH PARAMETERS
#include < iostream >
using namespace std;
class myclass
{
int a;
Public:
myclass (int x)
Void show( );
};
myclass : : myclass (int x)
{
Cout<<“In constructorn”;
a=x;
}
40
Void myclass : :show( )
{
Cout<<a<<“n”;
}
int main( )
{
myclass ob(4);
Ob.show( );
return 0;
}
#include <iostream. h>
#include <conio. h>
Class sum
{
Private:
int n, m ,s;
Public:
sum( int x, int y)
{
n=x;
m=y;
s=n + m;
} 41
psum( )
{
Cout<<“sum of ”<<n<<“and”<<m
<<“=“<<s<<endl;
}
};
Void main( )
{
sum a(16,10), b(2, 3);
a. psum( );
b. psum( );
getch( );
}
Output
Sum of 16 and 10= 26
Sum of 2 and 3= 5
CONSTRUCTOR OVERLOADING
 When more than one constructor functions are defined. Each constructor is defined
with a different set of parameters.
 Defining more than one constructor with different set of parameters is called
constructor overloading.
 Constructor overloading is used to initialize different values to class objects.
 When a program that uses the constructor overloading is compiled, C++ compilers
checks the number of parameters, their order and data types and marks them
differently.
 When an object of the class is created, the corresponding constructor that
matches the number of parameters of object function is executed. 42
#include <iostream. h>
#include <conio. h>
Class sum
{
Public:
Sum ( int i, int m, int n)
{
Cout<<“sum of 3 integer is =”<< i+m+n<< endl;
}
Sum ( int i, int m)
{
Cout<<“sum of 2 integer is =”<< i+m<< endl;
}
43
void main( )
{
Sum x(6,2) , y(7, 6, 8);
getch ( );
}
#include <iostream. h>
#include <conio. h>
Class find
{
Private:
int mx;
Public:
Find(int x, int y, int z)
{
If(x > y)
if(x > z)
mx= x;
else
mx=y;
44
Else if (y > z)
mx= y;
Else
mx=z;
Cout<<“Maximum value
between 3 is
”<<mx<<endl;
};
Find (int x, int y)
{
If(x > y)
mx=x;
Else
mx =y;
Cout<<“Maximum value
between 2 numbers is”
<<mx<<endl;
}
};
Void main( )
{
Clrscr ( );
int a=9, b=56, c =67;
Find ddd(a, b), gg(c, b, a);
Getch( );
}

Lecture 2 (1)

  • 1.
  • 2.
     Class isthe most important feature of C++ that supports object oriented programming (OOP).  In OOP, program is divided into objects.  OOP is an easy and flexible approach for designing and organizing the program.  A program is designed by using classes.  A class is a collection of data(data members) and functions (member functions or methods).  The data items and functions are defined within the class. 2
  • 3.
     The functionsare written to work upon the data items and each function has a unique relationship with the data items of the class.  Classes are defined to create user defined data types.  Definition of the data types does not create any space in the computer memory.  When a variable of that data type is declared, a memory space is reserved for that variable. but when a class is defined, it does not occupy any space in the computer memory.  Class defines the data item and the member function that can be used to work upon its data items.  Defining a class only specifies its data members and the relationship between the data items through its functions. 3
  • 4.
    SYNTAX OF CLASS ClassClass_name { Body of the class }; class class-name { private data and functions access-specifier: data and functions } object-list; (optional) 4
  • 5.
    Class : Akeyword used to represents a class Class_name: represents the name of the class Body of Class: The body of a class consist of the data items and the functions. These are called members of the class these are written between braces. Semicolon (;) The body of a class end with semicolon. 5
  • 6.
    #include <iostream.h> // usingnamespace std; class myclass { public: int i, j, k; // accessible }; int main() { myclass a, b; a.i = 100; a.j = 4; a.k = a.i * a.j; b.k = 12; cout << a.k << " " << b.k; return 0; } 6
  • 7.
    MEMBER OF ACLASS  A class contains data items (variable) and functions. Theses are called member of the class. The data item are data members and the function are called member functions.  By default all functions and variables declared inside a class are private to that class.  To declare public variable ,Keyword Public is used  Data members Class abc { int w, x, y, z; float a, b; }; Where a, b, w, x, y and z are the data members of the class “abc”. 7
  • 8.
    MEMBER FUNCTIONS  Thefunction of the class that are defined to work on its data members are called member functions of the class.  Member functions can be defined within the class or outside. 8
  • 9.
    // A Simpleclass declaration Class myclass { //private to myclass int a; Public: Void set_a (int num); Int get_a(); };  myclass contains one private variable ,a.  Two public functions set_a( ) and get_a ( ).  Function are declared within a class, are called member functions.  Variable a is private and cannot be accessible by any code outside myclass. 9
  • 10.
    Class xyz { private: Int a,b, c; Public: Void get (void) { Cout<<“enter value for a , b and c”; Cin>>a>>b>>c; } Void put (void) { Cout<<“a = ”<<a<<endl; Cout<<“b= ”<<b<<endl; Cout<<“c = ”<<c<<endl; } }; In this class   Three data members ◦ (a, b, & c)  Two member functions ◦ (“get” , ”put”) “get” function is used to input values into data members a , b ,& c. “Put” function is used to print values of the data members. 10
  • 11.
    MEMBER ACCESS SPECIFIERS The commands that determine whether a member of a class can be accessed from outside the class or not are called member access specifiers.  There are three types of member access specifiers:  Private  Public  Protected 11
  • 12.
    PRIVATE SPECIFIERS: • Themembers of a class that can be accessed only within the class are called private members. Private specifiers cannot can accessed from outside that class. • Normally all data members are declared as private. • Members functions can also be declared as private. • The default access mode is private. If no access specifiers is given . The members are treated as private. 12
  • 13.
    PUBLIC SPECIFIERS:  Publicmembers of the class can be accessed both inside and outside from the class.  The member functions and data members both are declared as public.  The protected access specifier is needed only when inheritance is involved. 13
  • 14.
    Class cdate { Private: Int y,d, m; Public: Void gdate(void) { Cout<<“enter year ?”; Cin>>y; Cout<<“enter month?”; Cin>>m; Cout<<“enter day?”; Cin>>d; Void pdate (void) { Cout<<d<<“/”<<m<<“/”<<y; } }; 14
  • 15.
     In theabove class:  “gdate” and “pdate” are the two member functions used in class.  Both member functions are declared public. 15
  • 16.
    OBJECTS  The variableor instances of a class are object.  A class may contain several data items and functions.  Combining of data and member functions into one unit called encapsulation.  An object represents the data members of a class in the memory. Each object of a class has a unique name.  Function in an object are called the member functions. They are known as the methods.  The member function are used to process and access data of a object. 16
  • 17.
    DECLARING OBJECTS OFA CLASS  The objects of a class are declared in the similar way as the variables of any data or structure types are declared.  While defining a class no space is reserved for it the memory but when an object of a class is declared, a memory space is reserved for that objects.  Syntax to create objects of a class: Class_ name object_name; e.g. awkum cs, phy, chem; 17
  • 18.
    CALLING MEMBER FUNCTIONS The member functions of a class is called through an object of the class.  The DOT operator is used .  The Dot operator Connects the Object name and the member function.  The General syntax of Calling a member function is : Object_name. member_function ( with argument in case of a function) For example , if “add” is the name of the object and “Pdate” is the member function than the meber function is called as shown below: add.pdate ( ); Only those member functions can be accessed from outside the class with the dot operator that have been declared as public 18
  • 19.
    #include<iostream. h> #include<conio. h> Classedate { private: int y, m, d; public: void gdate (void) { cout<<“Enter year ?”; cin>>y; cout<<“Enter Month?”; cin>>m; cout<<“Enter Day?”; cin>>d; } Void pdate (void) { cout<<“Date is: “<<endl; cout<<d<<“/”m<<“/”<<y; } }; Void main() { edate aniv; Clrscr (); aniv.gdate (); aniv.pdate(); getch(); } edate= 3 data members Y, n, d 2 member function “gdate” & “pdate” Output of program Enter year? 1980 Enter Month ?1 Enter Day ? 27 Date is : 27/1/1980 19
  • 20.
    WRITE A PROGRAMBY USING A CLASS TO INPUT TWO VALUES USING A MEMBER FUNCTION OF A CLASS . DISPLAY THE SUM OF THE TWO VALUES BY USING ANOTHER MEMBER FUNCTION OF THE CLASS #include<iostream. h> Class sum { Private: int n, m; Pubic : Void get (int a, int b) { n=a; m=b; } Void display (void) { Cout << “sum=“ <<n + m; } }; Void main() { sum s; int x, y; Cout<<“Enter first number ?”; Cin >> x ; Cout <<“Enter second number ?”; Cin >> y; s. get (x, y); s. display(); } 20
  • 21.
    Write a programby using a class employee to input the record of employees  Define the following data members: name, bpay, h_rent, gpay  Define the following member functions: input data in name & bpay calculate h _rent , ma, gpay print the complete record on the computer system  h_rent= house rent =60% of bpay ma= medical allowance =20% of bpay gpay = bpay + h_rent +ma 21
  • 22.
    #include<iostream. h> #include <conio.h> Classemployee { Private: Char name [15]; Float bpay. h_rent, ma, gpay; Public: Void input (void) { Cout<< “Enter name of Employee ?”; Cin>>name; Cout<<“Enter basic pay of employee?”; Cin>>bpay; } Void allow (void) { h_rent = bpay *(60/100); ma= bpay* (20/100); gpay= bpay+ h_rent + ma; } Void display (void) { Clrscr (); Cout<<“Name of employee:”<<name<<endl; Cout<<“Basic pay:”<<bpay<<endl; Cout<<“House rent:”<<h_rent<<endl; Cout<<“Medical allowance:”<<ma<<endl; Cout<<“Net pay:”<<gpay<<endl; } }; Void main ( ) { Employee obj1; Clrscr( ) obj1.input ( ); obj1.allow( ); Obj1.display ( ); getch(); } 22
  • 23.
    #include <iostream. h> #include<conio.h> Class student { Private : Char name [15]; Float s1, s2, s3, total, avg; Public: Void get_data(void) { Cout<<“Enter name of the student ?”; Cin>>name; Cout<<“Enter marks of 1st subject?”; Cin>>s1; } Write a class to input the name of student and marks of three subjects. Calculate the total marks and average marks. Cout<<“Enter marks of 2nd subject?”; Cin>>s2; Cout<<“Enter marks of 3rd subject?”; Cin>>s3; total = s1+ s2+ s3; avg = total / 3; } Void show (void) { Clrscr (); Cout<<“name of student :”<< name; Cout<<endl; Cout<<“marks of 1st subject:”<<s1; Cout<<endl; Cout<<“marks of 2nd and 3rd subject:”<< s2 << s3; Cout<< “total marks”<<total; Cout<<endl; Cout<< “average marks”<<avg; Cout<<endl; } }; Void main ( ) { Student abc; abc.get _data( ); abc.show ( ); getch( ); } 23
  • 24.
    DEFINING MEMBER FUNCTIONOUTSIDE THE CLASS  A member function of a class can also be defined outside the class.  The prototype of the function is declared inside the class.  The member function are defined outside the class in the similar way as user defined function are defined.  The Scope Resolution operator (: :) is used in member function declarator to define the function of class outside the class Syntax of member function definition outside the class: type class_name : : function_name (arguments) { body of function } 24
  • 25.
    Type represents thedata type of value returned by the member function Class_name represents the class name to which the member function belong : : double colon represents the scope resolution operator that is used to define the body of the member function of the specified class Function_name represents the name of the member function of a class with arguments (if any) Body of the function the set of statements of the functions The member function is defined outside of its class only if the body of the function definition is large. Otherwise the function definition should be defined inside the class 25
  • 26.
    WRITE A PROGRAMTO INPUT AND PRINT THE RECORD OF AN EMPLOYEE BY DEFINING MEMBER FUNCTION OUTSIDE THE CLASS #include <iostream. h> #include <conio.h> Class employee { Private : Char name [15]; Float bpay , h_rent , ma gpay; Public: Void input (void); Void allow (void); Void display(void); }; void main( ) { Employee obj; clrscr( ); obj. input ( ); obj. allow( ); 26 Obj. display ( ); } Void employee : : input(void) { Cout<<“enter name of employee? ”; Cin>>name; Cout<<“ enter basic pay of employee ?”; Cin >> bpay; } Void employee : : allow (void) { h_rent = bpay * (60/100); ma = bpay * ( 20/100); gpay= bpay + h_rent + ma ; } Void employee : : display (void) { Clrscr (); Cout<< “name of employee :”<< name<<endl; Cout<< “Basic pay :”<< bpay<<endl; Cout<< “house rent:”<<h_rent<<endl; Cout<< “medical allowance: ” << ma<<endl ; Cout<< “Net pay:”<< gpay<<endl ; }
  • 27.
    STORAGE OF OBJECTSIN MEMORY  When an object of a class is created, a memory is reserved in the computer memory to hold its data members.  Separate memory spaces are reserved for each class object.  The member function of a class are stored at only one location in the computer memory.  All objects of the class use the same member functions to process data.  Each object has separate memory space for data members.  The member function of the class are stored in only one place and are shared by all the objects of the class 27
  • 28.
    #include<iostream. h> #inlcude<conio. h> Classtemp Private : int x; Float y; Public : Void getdata (void) { Cout << “ enter value of x =”; Cin>> x; Cout << “ enter value of y =”; Cin>> y; } Void print (void) { Cout<<“enter value of X=”<<x<<endl; Cout<< “Enter value of y=”<<y<<endl; 28 } }; Void main( ) { temp a, b; Cout <<“get data for object a”<<endl; a.getdata ( ); Cout <<“get data for object b”<<endl; b. getdata ( ); Cout <<“Data in object a”<< endl; a. Print ( ); Cout <<“Data in object b”<< endl; b. Print ( ); getch( ); }
  • 29.
    The storage ofobject a and b as mentioned in the above program example is shown below. These object use the same number functions. 29 getdata() Print () Object a Object b x y x y
  • 30.
    CONSTRUCTOR  A constructoris member function of a class that is called and executed automatically when an object of that class is created.  Automatic initialization using an objects can be performed through constructor.  The name of the constructor function is same as the name of the class.  Any initialization that need to be performed on an object can be done automatically by the constructor function.  A constructor function may have arguments but it cannot return any value.  No return type 30
  • 31.
    # include <iostream.h> #include<conio.h> Class test { Public : test ( ) { Cout<<“welcome”<<endl; abc ( ) { Cout<<“OK”<<endl; }; 31 Void main( ) { test a, b, c; } Output of program Welcome welcome welcome
  • 32.
    #include <iostream. h> #include<conio. h> Class myclass { Public: int a; myclass ( ); Void show( ); }; myclass : : myclass( ) { Cout<<“In constructorn”; a=10; } 32 Void myclass : :show ( ) { Cout<<a; } int main( ) { myclass ob; ob. show ( ); return 0; }
  • 33.
     The valueof a is initialized by the constructor myclass( ).  The constructor is called when the object ob is created.  myclass( ) has no return type.(constructor does not return any value)  For global objects, an object’s constructor is called once, when the program first begins executions.  For local objects, the constructor is called each time the declaration statements is executed. 33
  • 34.
    INITIALIZING DATA USINGCONSTRUCTOR  The constructor function is normally used to initialize values in data members of a class when the program is executed.  This type of initialization is called the automatic initialization.  For example; If a member functions has two arguments of int type the specified value can be assigned to the data member of class using a constructor. 34
  • 35.
    #include <iostream. h> #include<conio. h> Class sum { Private: int n, m ,s; Public: sum( int x, int y) { n=x; m=y; s=n + m; } 35 psum( ) { Cout<<“sum of ”<<n<<“and”<<m <<“=“<<s<<endl; } }; Void main( ) { sum a(16,10), b(2, 3); a. psum( ); b. psum( ); getch( ); } Output Sum of 16 and 10= 26 Sum of 2 and 3= 5
  • 36.
    DESTRUCTOR  The complementof a constructor is destructor.  This function is called when an object is destroyed.  An object allocates memory when it is created. But when the destructor functions is called; the memory of the objects gets free.  The destructor functions has the same name as the name of the a class but tilde sign (~) is written before its name.  Like constructor, Destructor also don't return any value. 36
  • 37.
    #include<iostream.h> #include<conio.h> Class myclass { int a; Public: myclass(); //constructor ~myclass ( ); // destructor Void show( ); }; Myclass :: myclass ( ) { Cout<<“In constructor n”; a=10; } 37 myclass :: ~myclass ( ) { Cout<<“Destructing …n”; } Void myclass : : show ( ) { Cout<<a<<“n”; } int main( ) { myclass ob; ob.show( ); return 0; }
  • 38.
    #include <iostream.h> #include<conio.h> Class program { pubic: program() { cout<<“constructor functions ”<<endl; } ~program ( ) { cout<<“destructor function”<< endl; } }; 38 Void main( ) { Program x; int a, b, s; a=10; b=20; s= a + b; Cout<<“the sum numbers is ”<< s <<endl; getch( ); } Output constructor function Sum of two number is 30 destructor function
  • 39.
    #include <iostream> using namespacestd; class Counter { private: int count; //count public: Counter( ) //constructor { count=0; } void inc_count( ) //increment count { count++; } int get_count( ) //return count { 39 return count; } }; int main( ) { Counter c1, c2; //objects cout << “nc1=” << c1.get_count( ); //display cout << “nc2=” << c2.get_count( ); c1.inc_count( ); //increment c1 c2.inc_count( ); //increment c2 c2.inc_count( ); //increment c2 cout << “nc1=” << c1.get_count( ); //display again cout << “nc2=” << c2.get_count( ); cout << endl; return 0; } Robert Lafore BOOK 4th edition page :229 Output c1=0 c2=0 c1=1 c2=2
  • 40.
    CONSTRUCTOR WITH PARAMETERS #include< iostream > using namespace std; class myclass { int a; Public: myclass (int x) Void show( ); }; myclass : : myclass (int x) { Cout<<“In constructorn”; a=x; } 40 Void myclass : :show( ) { Cout<<a<<“n”; } int main( ) { myclass ob(4); Ob.show( ); return 0; }
  • 41.
    #include <iostream. h> #include<conio. h> Class sum { Private: int n, m ,s; Public: sum( int x, int y) { n=x; m=y; s=n + m; } 41 psum( ) { Cout<<“sum of ”<<n<<“and”<<m <<“=“<<s<<endl; } }; Void main( ) { sum a(16,10), b(2, 3); a. psum( ); b. psum( ); getch( ); } Output Sum of 16 and 10= 26 Sum of 2 and 3= 5
  • 42.
    CONSTRUCTOR OVERLOADING  Whenmore than one constructor functions are defined. Each constructor is defined with a different set of parameters.  Defining more than one constructor with different set of parameters is called constructor overloading.  Constructor overloading is used to initialize different values to class objects.  When a program that uses the constructor overloading is compiled, C++ compilers checks the number of parameters, their order and data types and marks them differently.  When an object of the class is created, the corresponding constructor that matches the number of parameters of object function is executed. 42
  • 43.
    #include <iostream. h> #include<conio. h> Class sum { Public: Sum ( int i, int m, int n) { Cout<<“sum of 3 integer is =”<< i+m+n<< endl; } Sum ( int i, int m) { Cout<<“sum of 2 integer is =”<< i+m<< endl; } 43 void main( ) { Sum x(6,2) , y(7, 6, 8); getch ( ); }
  • 44.
    #include <iostream. h> #include<conio. h> Class find { Private: int mx; Public: Find(int x, int y, int z) { If(x > y) if(x > z) mx= x; else mx=y; 44 Else if (y > z) mx= y; Else mx=z; Cout<<“Maximum value between 3 is ”<<mx<<endl; }; Find (int x, int y) { If(x > y) mx=x; Else mx =y; Cout<<“Maximum value between 2 numbers is” <<mx<<endl; } }; Void main( ) { Clrscr ( ); int a=9, b=56, c =67; Find ddd(a, b), gg(c, b, a); Getch( ); }