Object Oriented Programming Ashraf Zia Lecturer Abdul Wali Khan University, Mardan. Lecture - 1
Class A class is a sketch, blue print of an object. Keyword class is used to define the class. Example: Class my_class { body of the class; };
Members of the Class A class contains data item and functions. These are called members of the class. The data items are called data members of the class. The functions are called member functions of the class.
Data Members Data items are called data members of the class. For example, a class that has four integer type and two float type data items is declared as: Class abc { int w,x,y,z; float a,b; };
Member Functions Functions of the class that are defined to work on its data members are called functions of the class. Functions may be defined within the class or outside the class. For example: Class xyz { private: int a,b,c; Public: void getdata() { cout<<Enter Value of a,b,c; cin>>a>>b>>c; } void printdata() { cout<<a:<<a<<endl;cout<<b:<<b<<endl; cout<<c:<<c<<endl; } };
Member Access Specifiers Private: The members of a class that can be accessed only from within the class are called private members. They cant be accessed from outside the class. Normally all data members are declared as private. The member functions can also be declared as private. The members that are made private can only be accessed from within the class. Making a data to be accessed from within the class is called data hiding, i.e. the data declared as private is hidden from outside the class.
Public Specifier Public members can be accessed both from inside and outside the class. Normally member functions are declared as public. Data Members can also be declared as public.
Access Specifier Example Class cdate { Private: int y,d,m; Public: void get_date(void){ cout >y; cout >m; cout >d; } void print_date(void) { cout<<d<</<<m<</<<y; } };
Objects It is an instance of a class. The process of creating an object is called instantiation. When you create an object, some space is reserved for it in the memory. An object of a class consists of both data members and member functions. The combining of both data and functions in one unit is called Data Encapsulation.
Objects Continued… The functions in an object are called member functions. They are also known as methods. The member functions are used to process and access data of the object. The data from an object is accessed and processed through the member function of the object. It cannot directly read the data in the object. The data is hidden for other objects of the program. This is called data hiding.
Declaring Objects of a Class Objects of a class are declared in the similar way as the variables of any data or structure type are declared. When a class is defined, no space is reserved for it in the memory. It only provides information how its object will look like. When an object of a class is declared, a memory space is reserved for that object.
Syntax of Object declaration Class_name Object_names Separated by commas; For example: Car suziki, mercedes, alto, cultus, corolla;
Calling Member Functions Member functions are called in a similar way as member or data item of a structure is called. Member function is called through an object of a class. The dot operator is used for calling member functions. The dot operator connects the object name and the member function. The dot operator is also called the class member access operator. Only those member functions can be accessed from outside the class with the dot operator that have been declared as public.
Syntax for calling Member Function Object_name. MemberFunction_name[ with arguments in case of a function ] For example: Suzuki.start_engine(); Alto.close_door(); Mercedes.run();
Q: Write a Programe to Input date and Print on the screen by using Class. #include Class date { private: int y,m,d; public: void get_date(){ cout<<Enter Day, Month and Year; cin>>d>>m>>y; } void print_date(){ cout<<Date is:<<d<<m<<y; } }; main() { date my_date; my_date.get_date(); my_date.print_date(); }
Q: 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 Class sum { private: int m,n; public: void get_data(int a, int b){ n=a;m=b; } void print_data(){ cout<<Sum is:<<n+m; } }; main() { sum s; int x,y; cout >x; cout >y; s.get_data(x,y); s.print_data(); }
Q: Write a Program by using a class emp to input the record of employee. Employee name, Basic Pay, whose house rent is 60% of the basic pay, medical allowance is 20% of the basic pay and calculate the gross pay. #include Class emp { private: char name[15]; float bpay, h_rent, ma, gpay; public: void get_data(){ cout >name>>bpay; } void calculate(){ h_rent=bpay*60/100.0; ma=bpay*20/100.0; gpay=bpay+h_rent+ma; } void print_data(){ cout<<Name of Employee<<name<<endl; cout<<Basic Pay, House rent, Medical allowence<<bpay<<h_rent<<ma<<endl; cout<<Net pay<<gpay<<endl; }; main() { emp e; e.get_data(); e.calculate(); e.print_data(); }
Q: Write a Program by using a class emp to input name of the student and marks of three subjects, calculate the total marks and average marks. Each subject has a maximum of 100 marks. #include Class student { private: char name[15]; float s1, s2, s3, total, avg; public: void get_data(){ cout >name; cout >s1>>s2>>s3; total=s1+s2+s3; avg = total/3.0; } void print_data(){ cout<<Name of Student<<name<<endl; cout<<Average Marks:<<avg; }; main() { student std; std.get_data(); std.print_data(); }
Defining Member Functions Outside of the Class Member functions can also be defined outside of the class. In such case only the prototype of the member function is declared inside the class. Member functions are defined outside the class in similar way as user-defined functions are defined. The scope resolution operator (::) is used in the member function declarator to define the function of class outside the class.
Syntax for Declaring Member function outside the Class Type class_name:: function_name (arguments) { body of the function; }
Q: Write a Program by using a class emp to input the record of employee using member functions outside the class. Employee name, Basic Pay, whose house rent is 60% of the basic pay, medical allowance is 20% of the basic pay and calculate the gross pay. #include Class emp { private: char name[15]; float bpay, h_rent, ma, gpay; public: void get_data(void); void calculate_allow(void); void print_data(void); }; main() { emp e; e.get_data(); e.calculate_allow(); e.print_data(); }
Program Continued…. void emp::get_data(void){ cout >name>>bpay; } void emp::calculate_allow(void){ h_rent=bpay*60/100.0; ma=bpay*20/100.0; gpay=bpay+h_rent+ma; } void emp::print_data(void){ cout<<Name of Employee<<name<<endl; cout<<Basic Pay, House rent, Medical allowence<<bpay<<h_rent<<ma<<endl; cout<<Net pay<<gpay<<endl; }