Content text OOPS-UNIT-IV.pdf
3CS4-06: Object Oriented Programming Max. Marks: 150 (IA: 30, ETE: 120) SN CONTENTS Hours 1 Introduction to different programming paradigm, characteristics of OOP, Class, Object, data member, member function, structures in C++, different access specifiers, defining member function inside and outside class, array of objects. 8 2 Concept of reference, dynamic memory allocation using new and delete operators, inline functions, function overloading, function with default arguments, constructors and destructors, friend function and classes, using this pointer. 8 3 Inheritance, types of inheritance, multiple inheritance, virtual base class, function overriding, abstract class and pure virtual function 9 4 Constant data member and member function, static data member and member function, polymorphism, operator overloading, dynamic binding and virtual function 9 5 Exception handling, Template, Stream class, File handling. 6 Total 40 1) Constant Variables in C++ If you make any variable as constant, using const keyword, you cannot change its value. Also, the constant variables must be initialized while they are declared. int main { const int i = 10; const int j = i + 10; // works fine i++; // this leads to Compile time error } In the above code we have made i as constant, hence if we try to change its value, we will get compile time error. Though we can use it for substitution for other variables. 2) Pointers with const keyword in C++
Pointers can be declared using const keyword too. When we use const with pointers, we can do it in two ways, either we can apply const to what the pointer is pointing to, or we can make the pointer itself a constant. Pointer to a const variable This means that the pointer is pointing to a const variable. const int* u; Here, u is a pointer that can point to a const int type variable. We can also write it like, char const* v; still it has the same meaning. In this case also, v is a pointer to an char which is of const type. Pointers to a const variable is very useful, as this can be used to make any string or array immutable(i.e they cannot be changed). const Pointer To make a pointer constant, we have to put the const keyword to the right of the *. int x = 1; int* const w = &x; Here, w is a pointer, which is const, that points to an int. Now we can't change the pointer, which means it will always point to the variable x but can change the value that it points to, by changing the value of x. The constant pointer to a variable is useful where you want a storage that can be changed in value but not moved in memory. Because the pointer will always point to the same memory location, because it is defined with const keyword, but the value at that memory location can be changed. NOTE: We can also have a const pointer pointing to a const variable. const int* const x;
3) const Function Arguments and Return types We can make the return type or arguments of a function as const. Then we cannot change any of them. void f(const int i) { i++; // error } F(10); const int g() { return 1; } Some Important points to Remember 1. For built in datatypes, returning a const or non-const value, doesn't make any difference. 2. const int h() 3. { 4. return 1; 5. } 6. 7. int main() 8. { 9. const int j = h(); 10. int k = h(); } Both j and k will be assigned the value 1. No error will occur.
11. For user defined datatypes, returning const, will prevent its modification. 12. Temporary objects created while program execution are always of const type. 13. If a function has a non-const parameter, it cannot be passed a const argument while making a call. 14. void t(int*) 15. { 16. // function logic } If we pass a const int* argument to the function t, it will give error. 17. But, a function which has a const type parameter, can be passed a const type argument as well as a non-const argument. 18. void g(const int*) 19. { 20. // function logic } This function can have a int* as well as const int* type argument.