Object Oriented Programming Ashraf Zia Lecturer Abdul Wali Khan University, Mardan. Lecture - 2.

Презентация:



Advertisements
Похожие презентации
Object Oriented Programming Ashraf Zia Lecturer Abdul Wali Khan University, Mardan. Lecture - 1.
Advertisements

Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
Unit II Constructor Cont… Destructor Default constructor.
Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
Data Types in C. A Data Type A data type is –A set of values AND –A set of operations on those values A data type is used to –Identify the type of a variable.
1/27 Chapter 9: Template Functions And Template Classes.
A class is just a collection of variables--often of different types--combined with a set of related functions. The variables in the class are referred.
HPC Pipelining Parallelism is achieved by starting to execute one instruction before the previous one is finished. The simplest kind overlaps the execution.
1/30 Chapter 8: Dynamic Binding And Abstract classes.
1 © Luxoft Training 2012 Inner and anonymous classes.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Interface.
© Luxoft Training 2013 Annotations. © Luxoft Training 2013 Java reflection / RTTI // given the name of a class, get a "Class" object that // has all info.
Arrays Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
Multiples Michael Marchenko. Definition In mathematics, a multiple is the product of any quantity and an integer. in other words, for the quantities a.
Loader Design Options Linkage Editors Dynamic Linking Bootstrap Loaders.
Copyright 2003 CCNA 4 Chapter 11 Scaling IP Addresses By Your Name.
Basic Input - Output. Output functions printf() – is a library function that displays information on-screen. The statement can display a simple text message.
© 2009 Avaya Inc. All rights reserved.1 Chapter Two, Voic Pro Components Module Two – Actions, Variables & Conditions.
How can we measure distances in open space. Distances in open space.
1 C++ Structures Starting to think about objects...
Транксрипт:

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); }