Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:

Similar presentations


Presentation on theme: "Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:"— Presentation transcript:

1 Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public: ……// can be accessed by any }; class B : public A { private: …… protected: …… public: …… }; What does class B : public A mean? Based on an existing class A, we can create a new class B. If so, we have:  B is a derived class or subclass of A.  A is a parent class or superclass of B.

2 Message protocol class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public: ……// can be accessed by any void f(); int g(); float k(); …… }; Message protocol : the entire collection of methods of an object, i.e. { void A::f(), int A:: g(),float A:: f() … }

3 Instance variables, class variables etc. class A { private: int a; //instance variable protected: int x, y; //instance variable public: static int b; // class variable A() { // constructor, same name as class name a = b = 0; x = y = 0; } static int g() { return b; } // class method void f(int m, int n) { //instance method a+= m; b+= n; } }; int A::b = 2; // initialize class variable void main() { A a0; A*a1 = new A; a0.a = 2; // ok ? //Line K a0.b = 3; // ok? a1->x = 5; // ok? //Line L a0.f(1, 2); a1->f(3, 5); cout << a0.a << endl; cout << a0.b << endl; cout a << endl; cout b << endl; } Assume Lines K and L are removed. What are the outputs?

4 Instance variables, class variables etc. Instance variables: each instance of a class has its own set of instance variables. Class variables: all instances of a class share one copy each class variable. Example: A a0, a1, a2; 1.a0, a1, a2 share a single A::b i.e. a0.b, a1.b, a2.b are the same, A::b 2. a0.a, a1.a, a2.a are all different variables. class A { private: int a; //instance variable protected: int x, y; //instance variable public: static int b; // class variable A() { // constructor, same name as class name a = b = 0; x = y = 0; } static int g() { return b; } // class method void f(int m, int n) { //instance method a+= m; b+= n; } };

5 Object-oriented programming: C++ class A { private: int a; //instance variable static int b; // class variable protected: int x, y; public: A() { // constructor a = b = 0; x = y = 0; } void f() { a++; b++; } }; class B: public A { private: int x; //instance variable public: B() { // constructor c = 0; } void g() { x = 5; // A’s x or B’s x? } }; How to access A’s x in B’s methods?

6 Dynamic binding: virtual functions class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public: A(); virtual void f() = 0; // pure (abstract) method virtual void g() { cout << “A::g” << endl; } void k() { cout << “A::k” << endl; } }; class B : public A { public: B(); virtual void f() { … } // must define the pure method void g() { cout << “B::g” << endl; } //override A’s g void k() { cout << “B::k” << endl; } //override A’s k }; void main() { B b; A *c = new B; B *d = new B; b.g(); b.k(); c->g(); c->k(); d->g(); d->k(); } What are the outputs?

7 Dynamic binding class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public: A(); virtual void g() { cout << “A::g” << endl; } void k() { cout << “A::k” << endl; } }; class B : public A { public: B(); void g() { cout << “B::g” << endl; } void k() { cout << “B::k” << endl; } }; void main() { A a; B b; A *c = &b; B *d = new B; a.g(); a.k(); b.g(); b.k(); c->g(); c->k(); d->g(); d->k(); } What are the outputs?

8 Dynamic binding class A { private: public: A(); virtual void g() { cout << “A::g” << endl; } void k() { cout << “A::k” << endl; } }; class B : public A { public: B(); void g() { cout << “B::g” << endl; } void k() { cout << “B::k” << endl; } }; class C : public A { public: C(); void g() { cout << “C::g” << endl; } void k() { cout << “C::k” << endl; } }; void main() { A a; B b; C c; A *d; d = &a; d->g(); d->k(); d = &b; d->g(); d->k(); d = &c; d->g(); d->k(); } What are the outputs?

9 class A { private: public: void g(); }; class B { public: void f(); }; class C : public A, B { public: … … }; Multiple inheritance What happens if both A and B define functions with same name? Answer: Compilation error if C does not define them! A B C

10 class A { private: public: void g(); }; class B { public: void g(); }; class C : public A, B { public: //not define g() }; Error! Multiple inheritance class A { private: public: void g(); }; class B { public: void g(); }; class C : public A, B { public: void g(); }; OK! WHY?

11 void main() { A *a; B b; C c; a = &b; a->f(); a->g(); a->k(); a = &c; a->f(); a->g(); a->k(); c.f(); c.g(); c.k(); a->a = 5; cout << b.a << endl; cout << c.a << endl; } What are the outputs? class A { public: A() { } static int a; virtual void f() { cout << "A::f" << endl;} void g() {cout << "A::g" << endl;} void k() {cout << "A::k" << endl;} }; int A::a = 0; class B : public A { public: B() { } virtual void f() { cout << "B::f" << endl;} void g() {cout << "B::g" << endl;} void k() {cout << "B::k" << endl;} }; class C : public A, B { public: C() { } virtual void f() { cout << "C::f" << endl;} void g() {cout << "C::g" << endl;} void k() {cout << "C::k" << endl;} }; An exercise

12 void main() { A *a; B b; C c; a = &b; a->f(); a->g(); a->k(); a = &c; a->f(); a->g(); a->k(); c.f(); c.g(); c.k(); a->a = 5; cout << b.a << endl; cout << c.a << endl; } What are the outputs? class A { public: A() { } static int a; virtual void f() { cout << "A::f" << endl;} void g() {cout << "A::g" << endl;} void k() {cout << "A::k" << endl;} }; int A::a = 0; class B : public A { public: B() { } virtual void f() { cout << "B::f" << endl;} void g() {cout << "B::g" << endl;} void k() {cout << "B::k" << endl;} }; class C : public A, B { public: C() { } virtual void f() { cout << "C::f" << endl;} void g() {cout << "C::g" << endl;} void k() {cout << "C::k" << endl;} }; An exercise: solution B::f A::g A::k C::f A::g A::k C::f C::g C::k 5

13 Polymorphism If B is a subclass of A, we can do the following: A *a; a = new B; If B is not a subclass of A, we CANNOT write a = new B.

14 Polymorphism void main() { A a; B b; C c; D d; … a.draw(); b.draw(); c.draw(); d.draw(); … } class A { public: A() { } virtual void draw() { … } }; class B : public A { public: B() { } void draw() { … } }; class C : public A { public: C() { } void draw() { … } }; class D … class E… #define N 100 void main() { A *a[N]; a[0] = new A; a[1] = new B; a[2] = new C; … for (int i = 0; i < N; i++) { a[i]->draw(); }

15 Copyright © 2009 Addison-Wesley. All rights reserved.1-15 Are Subclasses Subtypes? Does an “is-a” relationship hold between a parent class object and an object of the subclass? – If a derived class is-a parent class, then objects of the derived class must behave the same as the parent class object A derived class is a subtype if it has an is-a relationship with its parent class – Subclass can only add variables and methods and override inherited methods in “compatible” ways

16 Copyright © 2009 Addison-Wesley. All rights reserved.1-16 Type Checking and Polymorphism Polymorphism may require dynamic type checking of parameters and the return value – Dynamic type checking is costly and delays error detection If overriding methods are restricted to having the same parameter types and return type, the checking can be static

17 Copyright © 2009 Addison-Wesley. All rights reserved.1-17 Instance Data Storage Class instance records (CIRs) store the state of an object – Static (built at compile time) If a class has a parent, the subclass instance variables are added to the parent CIR Because CIR is static, access to all instance variables is done as it is in records – Efficient


Download ppt "Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:"

Similar presentations


Ads by Google