Unit II Constructor Cont… Destructor Default constructor.

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



Advertisements
Похожие презентации
Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
Advertisements

1/27 Chapter 9: Template Functions And Template Classes.
1/30 Chapter 8: Dynamic Binding And Abstract classes.
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.
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.
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Object Oriented Programming Ashraf Zia Lecturer Abdul Wali Khan University, Mardan. Lecture - 2.
HPC Pipelining Parallelism is achieved by starting to execute one instruction before the previous one is finished. The simplest kind overlaps the execution.
1 © Luxoft Training 2012 Inner and anonymous 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.
© 2009 Avaya Inc. All rights reserved.1 Chapter Two, Voic Pro Components Module Two – Actions, Variables & Conditions.
Sequences Sequences are patterns. Each pattern or number in a sequence is called a term. The number at the start is called the first term. The term-to-term.
11 BASIC DRESS-UP FEATURES. LESSON II : DRESS UP FEATURES 12.
© 2005 Cisco Systems, Inc. All rights reserved. BGP v Route Selection Using Policy Controls Applying Route-Maps as BGP Filters.
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
Loader Design Options Linkage Editors Dynamic Linking Bootstrap Loaders.
Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.
Arrays Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
Initialization & Cleanup Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Polymorphism.
Транксрипт:

Unit II

Constructor

Cont…

Destructor

Default constructor

Example

Cont…

Dynamically created objects

Cont…

Operator Overloading Allow intrinsic operators in C++ to be applied to objects of new types. Overloading is achieved by defining functions named operatorXXX, where XXX is one of: Cant overload these: Cant change precedence, arity or associativity. Cant add new operators, either.

Operator Overloading (contd) Example: the subscript operator, which returns a reference. First, without operator overloading:

Operator Overloading (contd) Now, simply replace elem with operator[]:

Overloading Operators > We wish to use > for user- defined objects, the same way they are (normally) used for "built-in" objects (e.g. int, char, etc.).

Overloading Operators > Input is similar. The signature is: To use them:

Type conversion Reference conversions A reference conversion can be performed wherever a reference initialization occurs, including reference initialization done in argument passing and function return values. A reference to a class can be converted to a reference to an accessible base class of that class as long as the conversion is not ambiguous. The result of the conversion is a reference to the base class subobject of the derived class object. Reference conversion is allowed if the corresponding pointer conversion is allowed.

Arithmetic conversions and promotions Integral conversions Boolean conversion Floating point conversion Integral and floating point promotions

Pointer Conversion Qualification conversions An type-qualified rvalue of any type, containing zero or more const or volatile qualifications, can be converted to an rvalue of type-qualified type where the second rvalue contains more const or volatile qualifications than the first rvalue. An rvalue of type pointer to member of a class can be converted to an rvalue of type pointer to member of a class if the second rvalue contains more const or volatile qualifications than the first rvalue.

Cont… Function argument conversions When a function is called, if a function declaration is present and includes declared argument types, the compiler performs type checking. The compiler compares the data types provided by the calling function with the data types that the called function expects and performs necessary type conversions. For example, when function funct is called, argument f is converted to a double, and argument c is converted to an int: The automatic conversions consist of the following: 1.Integral and floating-point values are promoted. 2.Arrays or functions are converted to pointers. 3.Non-static class member functions are converted to pointers to members.

Cont… char * funct (double d, int i); /*... */ int main(void) { float f; char c; funct(f, c) /* f is converted to a double, c is converted to an int */ return 0; }

Explicit initialization with constructors A class object with a constructor must be explicitly initialized or have a default constructor. Except for aggregate initialization, explicit initialization using a constructor is the only way to initialize nonstatic constant and reference class members. A class object that has no constructors, no virtual functions, no private or protected members, and no base classes is called an aggregate. Examples of aggregates are C-style structures and unions. You explicitly initialize a class object when you create that object. There are two ways to initialize a class object: Using a parenthesized expression list. The compiler calls the constructor of the class using this list as the constructor's argument list. Using a single initialization value and the = operator. Because this type of expression is an initialization, not an assignment, the assignment operator function, if one exists, is not called. The type of the single argument must match the type of the first argument to the constructor. If the constructor has remaining arguments, these arguments must have default values.

Example illustrates explicit initialization #include using namespace std; class complx { double re, im; public: complx() : re(0), im(0) { } // default constructor // copy constructor complx(const complx& c) { re = c.re; im = c.im; } // constructor with default trailing argument complx( double r, double i = 0.0) { re = r; im = i; } void display() { cout << "re = "<< re << " im = " << im << endl; } };

Cont… int main() { // initialize with complx(double, double) complx one(1); // initialize with a copy of one // using complx::complx(const complx&) complx two = one; // construct complx(3,4) // directly into three complx three = complx(3,4); // initialize with default constructor complx four; // complx(double, double) and construct // directly into five complx five = 5; one.display(); two.display(); three.display(); four.display(); five.display(); }