Object Oriented Programming Ashraf Zia Lecturer Abdul Wali Khan University, Mardan. Lecture - 2
The :: Operator :: is known as the scope resolution operator. Example: #include Int a=10; Void main(){ int a=15; cout<<Local a:<<a; cout<<Global a:<<::a; ::a=20; cout<<Local a<<a; cout<<Global a<<::a;
Scope Resolution Operator If we have two variable with the same name. One is declared outside all the functions (global) and another is locally inside the function declared. Now if we attempt to access the variable in the function we access the local variable. Because the rule says that whenever there is conflict between global and local variables the local variable get the priority.
:: Operator Example #include int i=10; void main(){ int i=20; cout<<i<<::i; { int i=30; cout<<i<<::i; } cout<<i<<::i; }
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; }
Storage of Objects in Memory When an object of class is created space is reserved in the computer memory to hold its data members. Separate memory spaces are reserved for each class object. Member functions of the class are stored at only one place in the computer memory. Each object has separate memory space for data members. Member functions of class are stored in only one place and are shared by all objects of class.
Constructors A constructor is a member function of a class that is called and executed automatically when an object of that class is created. Name of the constructor function is same as the name of class itself. A constructor can have arguments but it cannot return any value.
Constructor Example #include Class car { public: car() { cout<<This is my Car; } }; main() { car alto, mehran, suziki; }
Initializing data using Constructors Constructor functions are usually used to initialize values in data members of a class when the program is executed. This type of initialization is called automatic initialization.
Constructor Initialization Example Class sum { private: int n, m, s; public: sum(int x, int y) { n=x; m=y; s=n+m; } print_sum() { cout<<Sum is:<<s; } }; main() { sum s1(5,6), s2(3,4); s1.print_sum(); s2.print_sum(); }
Constructor Overloading More than one constructor functions can be defined in one class. Each constructor function can have different set of parameters to initialize the data members. Defining more than one constructor with different set of parameters is called Constructor Overloading. When a program that uses the constructor overloading is compiled, C++ compiler checks the no of parameters, their order and data types and marks them differently.
Constructor Overloading Initialization Example Class sum { private: int n, m, s; public: sum(int no1, int no2, int no3) { cout<<Sum of 3 integers is:<<(no1+no2+no3); } sum(int x, int y) { cout<<Sum of 2 integers is:<<(x+y); } }; main() { sum s1(5,6), s2(3,4,6); }
Q: Write a Program to define two constructors to find out the maximum values. Class find { private: int max; public: find(int no1, int no2, int no3) { if(no1>no2) if(no1>no3) max=no1; else max=no3; elseif (no2>no3) max=no2; else max=no3; cout<<Maximum No is:<<max; } find (int x, int y) { if(x>y) max=x; else max=y; cout<<Maximum value is:<<max; } }; main() { find f1(5,6), f2(3,4,6); }
Destructors When an object is destroyed, a special member function of that class is executed automatically known as destructor function or destructor. Destructor has the same name as the name of a class. A tilde sign (~) is used before the name of destructor. Like constructors destructors do not return any value. Destructors have no arguments.
Destructors Continued… A local object is destroyed when all the statement of the function in which it is declared are executed. The destructor functions are commonly used to free the memory that was allocated for objects.
Constructor Destructor Example Class test { public: test() { cout<<I am constructor; } ~test() { cout<<I am destructor; } }; main() { test t1; int no1, no2; cout<<Sum is:<<(no1+no2); }
Passing Objects as Arguments Objects can also be passed as arguments to member functions. When an object is passed as an argument to a member function: Only the name of the object is written in argument. No of parameters and their types must be defined in the member function to which the object is to be passed. The objects that are passed are treated local for the member function and are destroyed when the control returns to the calling function.
Example of passing objects as Arguments #include Class test { private:char name[15]; public: void get_data() { cout<<Enter your name:; cin>>name; } void print_data(test s) { cout<<Name is:<<s.name; } }; main(){ Test temp, temp1; temp.get(); temp1.print_data(temp); }