Скачать презентацию
Идет загрузка презентации. Пожалуйста, подождите
Презентация была опубликована 9 лет назад пользователемЭмма Байкова
1 Unit II
2 Constructor
3 Cont…
4 Destructor
5 Default constructor
6 Example
7 Cont…
8 Dynamically created objects
9 Cont…
10 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.
11 Operator Overloading (contd) Example: the subscript operator, which returns a reference. First, without operator overloading:
12 Operator Overloading (contd) Now, simply replace elem with operator[]:
13 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.).
14 Overloading Operators > Input is similar. The signature is: To use them:
15 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.
16 Arithmetic conversions and promotions Integral conversions Boolean conversion Floating point conversion Integral and floating point promotions
17 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.
18 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.
19 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; }
20 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.
21 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; } };
22 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(); }
Еще похожие презентации в нашем архиве:
© 2024 MyShared Inc.
All rights reserved.