Скачать презентацию
Идет загрузка презентации. Пожалуйста, подождите
Презентация была опубликована 9 лет назад пользователемРимма Бачманова
1 Object Oriented Programming Ashraf Zia Lecturer Abdul Wali Khan University, Mardan. Lecture - 2
2
The :: Operator :: is known as the scope resolution operator. Example: #include Int a=10; Void main(){ int a=15; cout<
3 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.
4 :: Operator Example #include int i=10; void main(){ int i=20; cout<
5 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.
6 Syntax for Declaring Member function outside the Class Type class_name:: function_name (arguments) { body of the function; }
7 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(); }
8
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<
9 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.
10 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.
11
Constructor Example #include Class car { public: car() { cout<
12 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.
13
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<
14 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.
15
Constructor Overloading Initialization Example Class sum { private: int n, m, s; public: sum(int no1, int no2, int no3) { cout<
16
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<
17 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.
18 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.
19 Constructor Destructor Example Class test { public: test() { cout<
20 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.
21
Example of passing objects as Arguments #include Class test { private:char name[15]; public: void get_data() { cout<
Еще похожие презентации в нашем архиве:
© 2024 MyShared Inc.
All rights reserved.