Presentation is loading. Please wait.

Presentation is loading. Please wait.

OBJECT ORIENTED PROGRAMMING

Similar presentations


Presentation on theme: "OBJECT ORIENTED PROGRAMMING"— Presentation transcript:

1 OBJECT ORIENTED PROGRAMMING
Instructor: Rashi Garg Coordinator: Gaurav Saxena

2 Pointer to class members
C++ allows you to generate a special type of pointer that points generically to a member of a class, not to a specific instance of that member in an object. ::*, .*, ->* operators are used to create a pointer to class members and access the class members. Pointer to data member: <type> classname ::*<pointer-name> Pointer to member function: <ret-type>(classname::*<pointer name>)(<parameter List>)

3 example class cl { public: cl(int i) val=i; } int val; int double_val() return val+val; }; intmain() int cl::*data; // data member pointer int (cl::*func)();// function member pointer cl ob1(1), ob2(2); // create objects cl *p1; p1 = &ob1; // access objects through a pointer data = &cl::val; // get offset of val func= &cl::double_val; // get offset of double_val() cout<< p1->*data<< " “<< ob2.*data; cout<< "Here they are doubled: "; cout<< (p1->*func)() << " "; cout<< (ob2.*func)() << "\n"; return 0; }

4 Why copy constructor argument should be const???
When we create our own copy constructor, we pass an object by reference and we generally pass it as a const reference. One reason for passing const reference is, we should use const in C++ wherever possible so that objects are not accidentally modified

5 example Output: Compiler Error in line "Test t2 = fun();"
#include<iostream> using namespace std; class Test {    /* Class data members */ public:    Test(Test &t) { /* Copy data members from t*/}    Test()        { /* Initialize data members */ } }; Test fun()     cout << "fun() Called\n";     Test t;     return t; } int main()     Test t1;     Test t2 = fun();     return 0; Output: Compiler Error in line "Test t2 = fun();" The program looks fine at first look, but it has compiler error. If we add const in copy constructor, the program works fine, i.e., we change copy constructor to following. Test(const Test &t) { cout << "Copy Constructor Called\n"; } Or if we change the line “Test t2 = fun();” to following two lines, then also the program works fine. Test t2; t2 = fun();

6 Contd … The function fun() returns by value. So the compiler creates a temporary object which is copied to t2 using copy constructor in the original program (The temporary object is passed as an argument to copy constructor). The reason for compiler error is, compiler created temporary objects cannot be bound to non-const references and the original program tries to do that. It doesn’t make sense to modify compiler created temporary objects as they can die any moment.

7 Static data members in C++
#include <iostream> using namespace std; class A { int x; public: A() { cout << "A's constructor called " << endl; } }; class B static A a; B() { cout << "B's constructor called " << endl; } static A getA() { return a; } int main() B b; A a = b.getA(); return 0; } Output: Compiler Error: undefined reference to `B::a'

8 Correct Version #include <iostream> using namespace std; class A { int x; public: A() { cout << "A's constructor called " << endl; } }; class B static A a; B() { cout << "B's constructor called " << endl; } static A getA() { return a; } A B::a; // definition of a int main() B b; A a = b.getA(); return 0; } #include <iostream> using namespace std;  class A {     int x; public:     A() { cout << "A's constructor called " << endl;  } };  class B     static A a;     B() { cout << "B's constructor called " << endl; }     static A getA() { return a; }  A B::a;  // definition of a  int main()     A a = B::getA();   return 0; } Can be replaced by

9 Types of this pointer this pointer is passed as a hidden argument to all non-static member function calls. The type of this depends upon function declaration. If the member function of a class X is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*.

10 Use of initializer list
For initialization of non static const data member For initialization of reference members For initialization of member objects who donot have default constructor When constructor’s parameter name is same as data member For performance reason For initialization of base class members

11 1. For initialization of non static const data member
#include<iostream> using namespace std; class Test { const int t; public: Test(int t):t(t) {} //Initializer list must be used int getT() { return t; } }; int main() { Test t1(10); cout<<t1.getT(); return 0; } /* OUTPUT: 10 */

12 2. For initialization of reference member
// Initialization of reference data members #include<iostream> using namespace std; class Test { int &t; public: Test(int &t):t(t) {} //Initializer list must be used int getT() { return t; } }; int main() { int x = 20; Test t1(x); cout<<t1.getT()<<endl; x = 30; return 0; } /* OUTPUT:     20     30  */

13 3. For initialization of objects who do not have default constructor
#include <iostream> using namespace std; class A { int i; public: A(int ); }; A::A(int arg) { i = arg; cout << "A's Constructor called: Value of i: " << i << endl; } // Class B contains object of A class B { A a; B(int ); B::B(int x):a(x) { //Initializer list must be used cout << "B's Constructor called"; int main() {     B obj(10);     return 0; } /* OUTPUT:     A's Constructor called: Value of i: 10     B's Constructor called */

14 4. When constructors parameter name is same as data member
#include <iostream> using namespace std; class A { int i; public: A(int ); int getI() const { return i; } }; A::A(int i):i(i) { } // Either Initializer list or this pointer must be used /* The above constructor can also be written as A::A(int i) { this->i = i; } */ int main() { A a(10); cout<<a.getI(); return 0; /* OUTPUT: 10

15 5. For performance reasons
It is better to initialize all class variables in Initializer List instead of assigning values inside body. // Without Initializer List class MyClass { Type variable; public: MyClass(Type a) { // Assume that Type is an already // declared class and it has appropriate // constructors and operators variable = a; } }; Here compiler follows following steps to create an object of type MyClass 1. Type’s constructor is called first for “a”. 2. The assignment operator of “Type” is called inside body of MyClass() constructor to assign variable=a; 3. And then finally destructor of “Type” is called for “a” since it goes out of scope.

16 Contd … Now consider the same code with MyClass() constructor with Initializer List // With Initializer List class MyClass {     Type variable; public:     MyClass(Type a):variable(a) {   // Assume that Type is an already                      // declared class and it has appropriate                      // constructors and operators     } }; With the Initializer List, following steps are followed by compiler: 1. Copy constructor of “Type” class is called to initialize : variable(a). The arguments in initializer list are used to copy construct “variable” directly. 2. Destructor of “Type” is called for “a” since it goes out of scope. As we can see from this example if we use assignment inside constructor body there are three function calls: constructor + destructor + one addition assignment operator call. And if we use Initializer List there are only two function calls: copy constructor + destructor call.

17 6. For initialization of base class members
Parameterized constructor of base class can only be called using Initializer List. B::B(int x):A(x) { //Initializer list must be used     cout << "B's Constructor called"; } int main() {     B obj(10);     return 0; #include <iostream> using namespace std; class A { int i; public: A(int ); }; A::A(int arg) { i = arg; cout << "A's Constructor called: Value of i: " << i << endl; } // Class B is derived from A class B: A { B(int );

18 Copy constructor and overloaded assignment operator
Theassignment operatoris used to copy the values from one object to anotheralready existing object Cents cMark(5); // calls Cents constructor Cents cNancy; // calls Cents default constructor cNancy= cMark; // calls Cents assignment operator In this case, cNancyhas already been created by the time the assignment is executed. Consequently, the Cents assignment operator is called. The assignment operator must be overloaded as a member function What happens if the object being copied into does not already exist?

19 Contd … The copy constructor
Cents cMark(5); // calls Cents constructor Cents cNancy= cMark; // calls Cents copy constructor! The purpose of the copy constructor and the assignment operator are almost equivalent —both copy one object to another. However, the assignment operator copies to existing objects, and the copy constructor copies to newly created objects. If a new object has to be created before the copying can occur, the copy constructor is used. If a new object does not have to be created before the copying can occur, the assignment operator is used.


Download ppt "OBJECT ORIENTED PROGRAMMING"

Similar presentations


Ads by Google