410 likes | 524 Views
Course Title Object Oriented Programming with C++. instructor ADEEL ANJUM. بسم الله الرحمن الرحيم. Chapter No: 07. classes. BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER). 1. CLASSES.
E N D
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM بسم الله الرحمن الرحيم Chapter No: 07 classes BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1
CLASSES The most important feature of c ++ programming language is that it supports object oriented programming. In oop the program is divided into objects. oop is an easy and flexible approach of designing and organizing the program. In this chapter the concepts of classes and objects are discussed .
CLASSES A class is a collection of data and functions. The data items and functions are defined within the class. The function are written to work upon the data items and each function has a unique relationship with the data items of the class.
Conti… class are defined to create user defined data types. These are similar to built in data types available in all programming languages. When a class is defined, it does not occupy any space in the computer memory. It only defines the data items and the member function that can be used to work upon its data items.
DEFINING A CLASS A class is defined in a similar way as a structure is defined. The keyword “class” is used to define the class. The general syntax to define a class is : class class-name { body of class}; class: it is the keyword that is used to define a class.
Conti…… class-name :it represent the name of class. The rules to give the name of a class and a variable are same. Any word that can be a variable name in a c ++.
conti… body of class: The body of a class consists 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 ends with semicolon.
MEMBER S OF A CLASS A class contains data items and functions. These are called members of the class. The data items are called data members and the functions are called member function.
DATA MEMBERS The data items of a class are called data member of the class, for example : A class that has four integer type and two float type data item is declared as
conti.. class abc { int w , x , y , z; float a , b ; } ; in the above class a , b , w, w, y and z are data members of the class “abc”.
MEMBER FUNCTIONS the function of a class that are defined to work on its data members are called member functions of the class. the member functions may be defined with a class or out side it. for example:
conti… class xyz { private : int a, b , c ; Public : void get(void) { cout <<“ Enter value of a , b and c “; cin>> a>>b>>c; void pout(void) { cout<<“a=“<<a<<endl; cout<<“b=“<<b<<endl; cout<<“c=“<<c<<endl; } };
In the above class , there are three data member and two member functions. The member function are “get” and “pout”. The “get” function is used to input value into data member a, b and c. The “pout “ function is used to print value of the data members on the computer screen.
MEMBER ACCESS SPECIFIERS The commands that determines whether a member of a class can be accessed from out side the class or not are called member access specifies. Normally two types of member access specifies are used. These are : 1.private: 2.public:
PRIVATE SPECIFIER The member of a class that can be accessed only from within the class are called private members. They can not be accessed from out side the class. normally all data members are declared as private. The members that are made private can only be accessed from within the class.
Conti… A class member that come after the specifier private : and up to the next member access specifier are declared as a private and or accessible only from inside the class. The default access mode is private. Thus if no access specifier is given, the members are treated as a private.
PUBLIC SPECIFIER making a data to be access from within the class is called data hiding. i.e. the data declared as private is hidden from outside the class. public members of a class can be accessed both from inside and from out side the class.
CONTI.. Normally member functions are declared as public. Data members can also be declared as a public. The following example explain the use of member access specifier. in c ++.
class cdate { private : int y,m,d; public : void gdata() { cout<<“year ?”; cin>>y; cout<<“month”; cin>>m; cout<<“days”; cin>>d; } void pdata() { cout<<d<<“/”<<m<<“/”<<y;} };
Conti……. in the above example, the class has been defined its name is “cdate”. It has three data members y,m,d of integer type and has been declared as a private. These data members can only be accessed from inside this class.
Conti…… The class also has two members functions “gdate” and “pdate”. The “gdate” function get date and “pdate” function print the date in date format. (dd / mm / yyyy) on the computer screen. Both the function member are declared as a public.
OBJECTS A data type is used to declare a variable. A variable of a data type is also known as the instance or case of that data type . Each variable has a unique name but each variable follows the rules of its data type. When a variable of a data type is declared some space is reserved for it in the memory.
conti…. A class is also like a data type. It is therefore used to declare variable or instance. The variable or instance of a class are called objects. A class may contain several data items and function. Thus the objects of the class consists of both the data members and the member functions of the class.
conti… The combining of both the data and the function into one unit is called data encapsulation. An object represent data members of a class in the memory. Each object of a class has a unit name . The name of an object differentiate it from other objects of the same class.
Conti….. • The values of data members of different objects may be different or same . The values of data members in an objects are known as the state of the object. • The functions in an object are called the members functions. They are also known as the method. The member functions are used to process and access data of the object.
conti… For example the data from an object is accessed and processed through the member functions of the objects. It can not directly read the data in the object. The data is hidden for other objects of the program this is called data hiding. the data hiding and data encapsulation are the main features of object oriented programming.
DECLARING OBJECTS OF A CLASS The objects of a class are declared in the similar way as the variables of any data type are declared. when a class is defined ,no space is reserved for it in memory. It only provide information how its object will look like. When an object of a class is declared, a memory space is reserved for that object.
The syntax to create objects of class type is: class_name object_name separated by commas; For example, to define an object “b” of class “oop” the statement is written as: oop b; in the above statement one object “b” is declared of class “oop”. It is the declaration of an object that actually creates an object in the memory.
CALLING MEMBER FUNCTION The member function of a class is called in a similar way as member data is called. The member function is called through an object of the class. The dot operator is used. The dot operator connects the object name and the member function.
Syntax to call a member function object name .member function; For example ,if “add” is the name of the object and “pdate” is the member function then the member function is called as: add.pdate(); The dot operator is called the class member access operator.
#include<iostream.h> #include<conio.h> Class edate { private: int y , m , d; public: void gdate(void); { cout<<“Enter year?”; cin>>y; cout<<“Month”; cin>>m; cout<<“Enter Day”; cin>>d; } void pdate(void) { cout<<“Date is:”<<endl; cout<<d<<“/”<<m<<“/”<<y; } }; main(){ edate ani; clrscr(); ani.gdate(); ani.pdate(); }
Conti…… out put of program Enter year 1965 Enter month 1 Enter day 27 date is : 27/1/1965
In the above program the class “edate” has three data member y, m, d of int type and two member function “gdate” and “pdate”. The definition of both functions are inside the class “edate”. The function “gdate” inputs the values of year ,month and day. The function “pdate” prints the date in the date formate.
void main() { sum s; int x , y; cout<<“Enter first no”; cin>>x; cout<<“Enter second no”; cin>>y; s.get(x , y); s.display(); getch(); } #include<iostream.h> #include<conio.h> class sum { private: int n,m; public: void get (int a , int b) { n=a; m=b; } void display (void) { cout<<“Sum=”<<n+m; } };
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(void) { emp dd; clrscr(); dd. allow(); dd.display(); getch(); } #include<iostream.h> #include<conio.h> class emp { private: char name[15]; float bpay , h_rent ,ma , gpay; public: void input (void) { cout<<“Enter name of employee” cin>>name; cout<<“Basic pay”; cin>>bpay; } void allow (void) { h_rent = bpay * 60/100; ma = bpay * 20/100; gpay =bpay + h_rent + ma; }
void show() { clrscr(); cout<<“Name of student:”<<name<<endl; cout<<“Marks of oop:”<<oop<<endl; cout<<“Marks of dld:”<<dld<<endl; cout<<“Total marks:”<<tot<<endl; cout<<“Average:”<<avg<<endl; } }; void main () { std abc; clrscr(); abc.getrec(); abc. show(); getch(); } #include<iostream.h> #include<conio.h> class std { private: char name[15]; float oop , dld , tot , avg ; public: void getrec(void) { cout<<“Enter name of student”; cin>>name; cout<<“Enter oop marks”; cin>>oop; cout<<“Enter dld marks”; cin>>dld; tot = oop + dld; avg = tot / 3.0; }
DEFINING MEMBER FUNCTION OUTSIDE THE CLASS Member function of a class can also be defined outside the class. In this case, only the prototype of the member function is declared inside the class. The member functions are defined outside the class in the similar way as user defined functions are defined. However ,the scope resolution operator (::) is used in the member function declarator to define the function of class outside the class.
Syntax type class name:: function name() { body of function } TYPE : represent the data type of value returned by the member function. CLASS NAME: represent the class name to which the member function belongs.
Conti… :: double colon represents scope resolution operator that is used to define the body of member function of a specified class. FUNCTION NAME: represents the name of the member function of a class with argument . BODY OF FUNCTION: it is set of statements of the function.
void emp :: input (void) { cout<<“Enter name of employee” cin>>name; cout<<“Basic pay”; cin>>bpay; } void emp:: 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 emp:: allow (void) { h_rent = bpay * 60/100; ma = bpay * 20/100; gpay =bpay + h_rent + ma; } #include<iostream.h> #include<conio.h> class emp { private: char name[15]; float bpay , h_rent ,ma , gpay; public: void input(void); void allow(void); void display(void); }; void main(void) { emp dd; clrscr(); dd. allow(); dd.display(); getch(); }