OOP1 - int y; public: D(int a,int b) :C(a) {y=b;} int...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CSC 4101: Programming Languages 1 1 Object-Oriented Programming Textbook Chapter 9 Objects struct S { int x; float y; }; S a; a.x = 42; a.y = 3.14; S * p = &a; S * q = new S; int i = p->x; int j = q->x; a: x = 42 y = 3.14 p: q: 2 Methods struct S { int x; float y; int getX() { return x; } }; S * p = new S; p->x = 42; p->y = 3.14; int i = p->getX(); 3
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSC 4101: Programming Languages 2 Constructors struct S { int x; float y; S(int a, float b) { x = a; y = b; } int getX() { return x; } }; S * p = new S(42, 3.14); int i = p->getX(); 4 Visibility Specifiers struct S { private: int x; float y; public: S(int a, float b) { x = a; y = b; } int getX() { return x; } }; S * p = new S(42, 3.14); int i = p->getX(); int j = p->x; 5 struct vs. class struct S { private: = ... }; class S { public: = ... }; class S { ... }; struct S { ... }; 6
Background image of page 2
CSC 4101: Programming Languages 3 Inheritance class C { int x; public: C(int a) { x = a; } int getX() { return x; } }; class D : public C {
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: int y; public: D(int a,int b) :C(a) {y=b;} int getY() { return y; } }; C * p = new C(42); D * q = new D(42, 3); p: x = 42 q: x = 42 y = 3 7 Subtyping class D : public C { . .. }; C * q = new D(42, 3); q: x = 42 C part y = 3 A D object can be used wherever a C object is expected. 8 Virtual Methods class C { protected: int x; public: C(int a) { x = a; } virtual void print() { cout << x; } }; class D : public C { int y; public: D(int a, int b) : C(a) { y = b; } virtual void print() { cout << '(' << x << ", " << y << ')'; } }; C * p = new D(42, 3); p->print(); 9 CSC 4101: Programming Languages 4 Virtual Methods Conceptually p: x = 42 print D::print y = 3 10 Virtual Methods Actual Implementation p: x = 42 C_D_vtable: _vtbl y = 3 D::print p->print() is translated to ((*(p->_vtbl))[1])(p) 11...
View Full Document

This note was uploaded on 12/31/2011 for the course CSC 4101 taught by Professor Kraft,d during the Fall '08 term at LSU.

Page1 / 4

OOP1 - int y; public: D(int a,int b) :C(a) {y=b;} int...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online