Download presentation
Presentation is loading. Please wait.
Published byLinette Flynn Modified over 9 years ago
1
OBJECT ORIENTED PROGRAMMING Instructor: Rashi Garg Coordinator: Gaurav Saxena
2
Size of Empty Class 1. #include using namespace std; class Empty {}; int main() { cout << sizeof(Empty); return 0; } Output: 1 Size of an empty class is not zero. It is 1 byte generally. It is nonzero to ensure that the two different objects will have different addresses 2. #include using namespace std; class Empty { }; int main() { Empty a, b; if (&a == &b) cout << "impossible " << endl; else cout << "Fine " << endl; return 0; } Output: Fine
3
Contd … For the same reason (different objects should have different addresses), “new” always returns pointers to distinct objects #include using namespace std; class Empty { }; int main() { Empty* p1 = new Empty; Empty* p2 = new Empty; if (p1 == p2) cout << "impossible " << endl; else cout << "Fine " << endl; return 0; } Output: Fine
4
Local Static Object #include class Test { public: Test() { std::cout << "Constructor is executed\n"; } ~Test() { std::cout << "Destructor is executed\n"; } }; void myfunc() { static Test obj; } // Object obj is still not destroyed because it is static int main() { std::cout << "main() starts\n"; myfunc(); // Destructor will not be called here std::cout << "main() terminates\n"; return 0; } Output: main() starts Constructor is executed main() terminates Destructor is executed
5
Global Static Object #include class Test { public: int a; Test() { a = 10; std::cout << "Constructor is executed\n"; } ~Test() { std::cout << "Destructor is executed\n"; } }; static Test obj; int main() { std::cout << "main() starts\n"; std::cout << obj.a; std::cout << "\nmain() terminates\n"; return 0; } Output: Constructor is executed main() starts 10 main() terminates Destructor is executed
6
To explicitly call Constructor & Destructor 1. #include using namespace std; class Test { public: Test() { cout << "Constructor is executed\n"; } ~Test() { cout << "Destructor is executed\n"; } }; int main() { Test(); // Explicit call to constructor Test t; // local object t.~Test(); // Explicit call to destructor return 0; } 2. Can be called from the member function of the class #include using namespace std; class Test { public: Test() { cout << "Constructor is executed\n"; } ~Test() { cout << "Destructor is executed\n"; } void show() { Test(); this->Test::~Test(); } }; int main() { Test t; t.show(); return 0; }
7
Constant Member Function When a function is declared as const, it can be called on any type of object. Non- const functions can only be called by non-const objects. #include using namespace std; class Test { int value; public: Test(int v = 0) {value = v;} int getValue() {return value;} }; int main() { const Test t; cout << t.getValue(); return 0; } This program has compiler errors
8
Can we use function on left side of expression in C++ ??? #include using namespace std; /* such a function will not be safe if x is non static variable of it */ int &fun() { static int x; return x; } int main() { fun() = 10; /* this line prints 10 on screen */ cout<<fun(); return 0; } Output: 10
9
Create a class whose objects can only be dynamically allocated #include using namespace std; // A class whose object can only be dynamically created class Test { private: ~Test() { cout << "Destroying Object\n"; } public: Test() { cout << "Object Created\n"; } friend void destructTest(Test* ); }; // Only this function can destruct objects of Test void destructTest(Test* ptr) { delete ptr; cout << "Object Destroyed\n"; } int main() { /* Uncommenting following following line would cause compiler error */ // Test t1; // create an object Test *ptr = new Test; // destruct the object to avoid memory leak destructTest(ptr); return 0; } Output: Object Created Destroying Object Object Destroyed
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.