Unit 1 I Polymorphism Inheritance Lecture 2 Pitfalls

  • Slides: 6
Download presentation
Unit 1 I Polymorphism & Inheritance Lecture 2

Unit 1 I Polymorphism & Inheritance Lecture 2

Pitfalls of Operator Overloading and Conversion � 1. Only existing operators can be overloaded.

Pitfalls of Operator Overloading and Conversion � 1. Only existing operators can be overloaded. � 2. The overloaded operator must have atleast one operand that is of �user defined data type. � 3. Overloaded operators follow the syntax rules of the original �operators. they cannot be overridden. � 4. There are some operators that cannot be overloaded. � 5. We cannot use friend function to overload certain operators.

Keywords Explicit and Mutable � EXPLICIT� 1. Constructor is for creating instance of class.

Keywords Explicit and Mutable � EXPLICIT� 1. Constructor is for creating instance of class. The name of � constructor is same as the class name. � 2. By default, the constructor are provided by the compiler but � the programmer can explicitly create it. � 3. In C++ the compiler is allowed to make one implicit � conversion to resolve the parameters to a function. Prefixing � the explicit keyword to the constructor prevents the � compiler from using that constructor for implicit � conversions.

IMPLICIT CONVERSION PROGRAM � � � � � #include<iostream> using namespace std; class test

IMPLICIT CONVERSION PROGRAM � � � � � #include<iostream> using namespace std; class test { int val; public: test (int x); val(x); { Cout<<”n val=”<<val; } }; Int main() { test obj=100; return o; } OUTPUT – VAL=100

USING EXPLICIT � � � � � #include<iostream> using namespace std; class test {

USING EXPLICIT � � � � � #include<iostream> using namespace std; class test { int val; public: explicit test (int x); val(x); { Cout<<”n val=”<<val; } }; int main() { test obj=100; return o; }

MUTABLE KEYWORD �Const class object �When the object is declared with const then its

MUTABLE KEYWORD �Const class object �When the object is declared with const then its data members cannot be �changed. �Mutable keyword – �It is used with the data members which we want to change the object of �that class is constant.