0% found this document useful (0 votes)
20 views40 pages

Lec 11 OOP

Uploaded by

Malk Al Masrea
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views40 pages

Lec 11 OOP

Uploaded by

Malk Al Masrea
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Chapter 14:

More About Classes

Copyright © 2012 Pearson Education, Inc.


14.1
Instance and Static
Members

Copyright © 2012
Copyright
Pearson ©
Education,
2012 Pearson
Inc. Education, Inc.
Class Members

Copyright © 2012 Pearson Education, Inc.


Instance and Static Members
• Instance variable: a member variable in a class. Each
object has its own copy.

• Static variable: one variable shared among all objects


of a class.

• Static member function: can be used to access static


member variable; can be called before any objects are
defined
Copyright © 2012 Pearson Education, Inc.
static member variable
Contents of Tree.h
1 // Tree class Static member declared here.
2 class Tree
3 {
4 private:
5 static int objectCount; // Static member variable.
6 public:
7 // Constructor
8 Tree()
9 { objectCount++; }
10
11 // Accessor function for objectCount
12 int getObjectCount() const
13 { return objectCount; } Static member defined here.
14 };
15
16 // Definition of the static member variable, written
17 // outside the class.
18 int Tree::objectCount = 0;

Copyright © 2012 Pearson Education, Inc.


Copyright © 2012 Pearson Education, Inc.
static member function

• Declared with static before return type:


static int getObjectCount() const
{ return objectCount; }
• Static member functions can only access static
member data
• Can be called independent of objects:
int num = Tree::getObjectCount();

Copyright © 2012 Pearson Education, Inc.


Modified Version of Tree.h
1 // Tree class
2 class Tree
3 {
4 private:
5 static int objectCount; // Static member variable.
6 public:
7 // Constructor
8 Tree()
9 { objectCount++; }
10
11 // Accessor function for objectCount
12 static int getObjectCount() const
13 { return objectCount; }
14 };
15
16 // Definition of the static member variable, written
17 // outside the class.
18 int Tree::objectCount = 0;

Now we can call the function like this:


cout << "There are " << Tree::getObjectCount()
<< " objects.\n";

Copyright © 2012 Pearson Education, Inc.


Chapter 15:

OOP Characteristics

Copyright © 2012 Pearson Education, Inc.


15.1
What Is Inheritance?

Copyright © 2012
Copyright
Pearson ©
Education,
2012 Pearson
Inc. Education, Inc.
What Is Inheritance?

• Provides a way to create a new class from an


existing class.

• The new class is a specialized version of the


existing class

Copyright © 2012 Pearson Education, Inc.


Example: Insects

Copyright © 2012 Pearson Education, Inc.


The "is a" Relationship
• Inheritance establishes an "is a"
relationship between classes.
– A poodle is a dog
– A car is a vehicle
– A flower is a plant
– A football player is an athlete

Copyright © 2012 Pearson Education, Inc.


Inheritance – Terminology and
Notation
• Base class (or parent) – inherited from
• Derived class (or child) – inherits from the base class
• Notation:
class Student // base class
{
. . .
};
class UnderGrad : public student
{ // derived class
. . .
};

Copyright © 2012 Pearson Education, Inc.


15.2
Protected Members
&
Class Access

Copyright © 2012
Copyright
Pearson ©
Education,
2012 Pearson
Inc. Education, Inc.
Protected Members and
Class Access
• protected member access specification:
like private, but accessible by objects of
derived class.

• Class access specification


determines how private, protected,
and public members of base class are
inherited by the derived class.

Copyright © 2012 Pearson Education, Inc.


Class Access Specifiers

1) public – object of derived class can be


treated as object of base class (not vice-
versa)
2) protected – more restrictive than public,
but allows derived classes to know details of
parents
3) private – prevents objects of derived class
from being treated as objects of base class.

Copyright © 2012 Pearson Education, Inc.


Inheritance vs. Access
How inherited base class
members
Base class members appear in derived class
private: x private x is inaccessible
protected: y base class
private: y
public: z private: z

private: x protected x is inaccessible


protected: y base class protected: y
public: z protected: z

private: x public x is inaccessible


protected: y base class protected: y
public: z public: z
Copyright © 2012 Pearson Education, Inc.
More Inheritance vs. Access
class Grade class Test : public Grade
private members: private members:
char letter; int numQuestions;
float score; float pointsEach;
void calcGrade(); int numMissed;
public members: public members:
void setScore(float); Test(int, int);
float getScore();
char getLetter();
private members:
int numQuestions:
When Test class inherits float pointsEach;
from Grade class using int numMissed;
public class access, it public members:
Test(int, int);
looks like this: void setScore(float);
float getScore();
float getLetter();

Copyright © 2012 Pearson Education, Inc.


More Inheritance vs. Access (2)
class Grade class Test : protected Grade
private members: private members:
char letter; int numQuestions;
float score; float pointsEach;
void calcGrade(); int numMissed;
public members: public members:
void setScore(float); Test(int, int);
float getScore();
char getLetter();
private members:
int numQuestions:
When Test class inherits float pointsEach;
from Grade class using int numMissed;
protected class access, it public members:
Test(int, int);
looks like this: protected members:
void setScore(float);
float getScore();
float getLetter();

Copyright © 2012 Pearson Education, Inc.


More Inheritance vs. Access (3)
class Grade class Test : private Grade
private members: private members:
char letter; int numQuestions;
float score; float pointsEach;
void calcGrade(); int numMissed;
public members: public members:
void setScore(float); Test(int, int);
float getScore();
char getLetter();
private members:
int numQuestions:
When Test class inherits float pointsEach;
from Grade class using int numMissed;
private class access, it void setScore(float);
float getScore();
looks like this: float getLetter();
public members:
Test(int, int);

Copyright © 2012 Pearson Education, Inc.


15.3
Constructors and
Destructors in Base
&
Derived Classes

Copyright © 2012
Copyright
Pearson ©
Education,
2012 Pearson
Inc. Education, Inc.
Constructors & Destructors in
Base and Derived Classes
• Derived classes can have their own constructors
and destructors.

• When an object of a derived class is created, the


base class’s constructor is executed first, followed
by the derived class’s constructor.

• When an object of a derived class is destroyed, its


destructor is called first, then that of the base
class.
Copyright © 2012 Pearson Education, Inc.
Constructors & Destructors in
Base and Derived Classes

Copyright © 2012 Pearson Education, Inc.


Constructors & Destructors in
Base and Derived Classes

Copyright © 2012 Pearson Education, Inc.


Constructors & Destructors in
Base and Derived Classes

Copyright © 2012 Pearson Education, Inc.


Constructors and Destructors in
Base and Derived Classes
Constructor initialization

Copyright © 2012 Pearson Education, Inc.


Constructors & Destructors in
Base and Derived Classes

Copyright © 2012 Pearson Education, Inc.


Constructors & Destructors in
Base and Derived Classes

Copyright © 2012 Pearson Education, Inc.


Passing Arguments to
Base Class Constructor
• Allows selection between multiple base
class constructors
• Specify arguments to base constructor on
derived constructor heading:
Square::Square(int side) :
Rectangle(side, side)
• Can also be done with inline constructors
• Must be done if base class has no default
constructor

Copyright © 2012 Pearson Education, Inc.


Passing Arguments to
Base Class Constructor

derived class constructor base class constructor

Square::Square(int
side):Rectangle(side,side)
derived constructor base constructor
parameter parameters

Copyright © 2012 Pearson Education, Inc.


15.4
Class Hierarchies

Copyright © 2012
Copyright
Pearson ©
Education,
2012 Pearson
Inc. Education, Inc.
Class Hierarchies
• A base class can be derived from another
base class.

Copyright © 2012 Pearson Education, Inc.


Class Hierarchies
• Consider the GradedActivity, FinalExam,
PassFailActivity, PassFailExam hierarchy in
Chapter 15.

Copyright © 2012 Pearson Education, Inc.


15.5
Multiple Inheritance

Copyright © 2012
Copyright
Pearson ©
Education,
2012 Pearson
Inc. Education, Inc.
Multiple Inheritance
• A derived class can have more than one base
class
• Each base class can have its own access
specification in derived class's definition:
class cube : public square,
public rectSolid;

class class
square rectSolid

class
cube

Copyright © 2012 Pearson Education, Inc.


Multiple Inheritance
• Arguments can be passed to both base
classes' constructors:
cube::cube(int side) :
square(side),
rectSolid(side, side, side);
• Base class constructors are called in order
given in class declaration, not in order
used in class constructor

Copyright © 2012 Pearson Education, Inc.


Multiple Inheritance

• Problem: what if base classes have member


variables/functions with the same name?
• Solutions:
– Derived class redefines the multiply-defined function
– Derived class invokes member function in a particular
base class using scope resolution operator ::
• Compiler errors occur if derived class uses base
class function without one of these solutions

Copyright © 2012 Pearson Education, Inc.


Encapsulation

Copyright © 2012 Pearson Education, Inc.


Thank You

Copyright © 2012 Pearson Education, Inc.

You might also like