Download presentation
Presentation is loading. Please wait.
Published byJeffrey Miller Modified over 8 years ago
1
Lecture 03b: C++ classes Topics: access modifiers basic inheritance
2
Part I: Access Modifiers 3 of them: public, private, protected – By default, private. Affects who can access the member – "Member" means attributes and methods. Modifier stays in effect until changed class Foo { int attribute1;// private public: float attribute2;// public void method1();// public protected: string attribute3;// protected public: int method2(int x);// public };
3
Access Modifiers, cont. Access modifiers play a role in inheritance too. Here, just looking at access – If not using inheritance, private and protected behave similarly. Rules: – public: this member can be accessed within the class definition and externally – private and protected: this member can only be accessed within the class definition.
4
Example class Foo { public: int x; string y; void func1() {cout << "Hello!" << endl;} void func3() {func2();} private: float z; void func2() {cout << "Hello!" << endl;} }; int main() { Foo f; // Note: main is "external" to the class, so we // can only access public members f.x = 9;// OK f.y = "ABC";// OK f.func1();// OK f.z = 3.2;// ERROR! f.func2();// ERROR! f.func3();// OK. Public, but indirectly calls // a private member }
5
"Getters" and "Setters" In good software design, you sometimes – Want to safeguard data – Hide implementation details from user (abstraction) – Determine an API and not change it afterwards Making attributes protected/private can help with these goals. At first, it will seem unnecessary: – Good habit to get into… If these are short, I usually do them inline.
6
Getter, Setter example // Note: a convenient way to make an alias for long names typedef unsigned int my_uint; class MyDate { public: MyDate(my_uint m, my_uint d, my_uint y) : mMonth(m), mDay(d), mYear(y) {} my_uint getMonth() { return mMonth; } void setMonth(my_uint m); void addMonth(); protected: my_uint mMonth, mDay, mYear; };
7
Getter, Setter example, cont. void MyDate::setMonth(my_uint m) { if (m = 1) mMonth = m; // else { ??? } Later: raise an EXCEPTION } void MyDate::addMonth() { mMonth++; if (mMonth > 12) { mMonth = 1; mYear++; } }
8
Part II: Basic Inheritance A means to create a new (child class) from an existing class (parent class) Advantages: – Avoids re-typing (and frees you from need to re- copy changes to parent class). – Allows definition of interface classes (later) – Allows polymorphism (later) – A good organizational tool
9
Ineritance overview Once you've created a child class, you: – Have access to all members of the parent class [Unless they were declared private] – Can add new members – Can replace (overload) inherited members We'll see that polymorphism plays a role here (later) When deriving a class, you can choose how to derive it: public, private, protected – I'll just use public inheritance (it's by far the most common)
10
Basic Inheritance example class Shape { public: Shape(int id) : mID(id) {} ~Shape() {} void getID() { return mID; } protected: int mID; }; class Circle : public Shape { public: Circle(int id, float rad) : Shape(id), mRad(rad) {} ~Circle(); float getRad() { return mRad; } protected: float mRad; };
11
Basic Inheritance example, cont. int main() { Shape s(15); Circle c(16, 3.9f);// Calls Shape::Shape, then // Circle::Circle cout << s.getID() << endl; cout << c.getID() << ", " << c.getRad(); } // Circle::~Circle called, then // Shape::~Shape (for c) // Just Shape::~Shape (for s)
12
Basic polymorphism [We'll look at this in more detail later] Basically, polymorphism is – The ability to re-define a method in a derived class. Simple example: class Parent {public: void Foo() { cout << "Parent::Foo\n"; } }; class Child : public Parent {public: // simple polymorphism void Foo() { cout << "Child::Foo\n"; } }; int main() { Parent p; Child c; p.Foo(); // prints 'Parent::Foo' c.Foo();// prints 'Child::Foo' }
13
Polymorphism, cont. Sometimes, though, we would like the parent class to call a re-defined method in the child class class Parent {public: void Foo() { cout << "Parent::Foo\n"; Bar();} void Bar() { cout << "Parent::Bar\n"; } }; class Child : public Parent {public: // We're inheriting Foo. We'd *like* it to call our Bar void Bar() { cout << "Child::Bar\n"; } }; int main() { Parent p; Child c; p.Foo(); // prints 'Parent::Foo', 'Parent::Bar' c.Foo();// prints 'Parent::Foo', 'Parent::Bar' // Why not 'Parent::Foo', 'Child::Bar'? }
14
Polymorphism, cont. The fix is to make Bar virtual – [Discuss Late-binding vs. Early-binding] class Parent {public: void Foo() { cout << "Parent::Foo\n"; Bar();} virtual void Bar() { cout << "Parent::Bar\n"; } }; class Child : public Parent {public: // We're inheriting Foo. We'd *like* it to call our Bar void Bar() { cout << "Child::Bar\n"; } }; int main() { Parent p; Child c; p.Foo(); // prints 'Parent::Foo', 'Parent::Bar' c.Foo();// prints 'Parent::Foo', 'Child::Bar' }
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.