OOP Week One-Two Material
Week One (Overview of Course): Step Assignments: About 6 Design Assignments: About 10
To Do: Read Chapter One in Object-Oriented Modeling Do Step 1 of the Assignment
Week Two: • Review basic OOP Concepts • Review Inheritance and Polymorphism • Work on “Square” program • Objects vs Classes • Constant Functions and Constructors • Pointers
Work on “Square” program
Let’s say we want to describe a square
Heres the header file
#ifndef CSQUARE_H_
#define CSQUARE_H_
class CSquare{
private:
int mX; // Center x
int mY; // Center y
int mWidth; // Width of the square
public:
CSquare() : mX(0), mY(0), mWidth(100){}
int GetX() const {
return mX;
}
void SetX(int x)
{
mX = x;
}
int GetY() const {
return mY;
}
void SetY(int y)
{
mY = y;
}
int GetWidth() const {
return mWidth;
}
void SetWidth(int w)
{
mWidth = w;
}
}
#endif /* CSQUARE_H_ */
What do these do?
- These are the #include Guard
#ifndef CSQUARE_H_
#define CSQUARE_H_
// code
#endif /* CSQUARE_H_ */
Objects vs Classes
Now what is the difference between the two?
-
Class: A type, which defines what an object will contain
-
Object: An instance of the class
Constant Functions and Constructors
-
const funciton is not allowed to change the object and is guaranteed not to.
-
{} is the code to run to construct an object of our class CSquare
Inheritance
-
Inheritance creates new classes based on the classes that have already been defined. The capabilities of the base class are inherited by the dervied class
-
For Shape class, we have many different shapes like squares, rectangles, circles, ovals, etc. What do they all have in Common? What is unique to each of them? This is where we could include Inheritance!
Advantages of Inheritance
- Factors out code that is common in multiple classes
- Dervied class inherits both function and data members from the base class
- Dervied class can add additional functions or data memebers
- Dervied class can override inherited function memebers with new methods
- Reuse functions that operate on a base-class objects
Example
class CShape{
private:
int mX; // Center x
int mY; // Center y
public:
CShape() : mX(0), mY(0),){}
int GetX() const {
return mX;
}
void SetX(int x)
{
mX = x;
}
int GetY() const {
return mY;
}
void SetY(int y)
{
mY = y;
}
}
// Two Derived Classes
// Square Class
class CSquare :public CShape
{
private:
int mWidth; // Width of the square
public:
CSquare(): mWidth(100){}
int GetWidth() const
{
return mWidth;
}
void SetWidth(int w)
{
mWidth = w;
}
}
// Oval Class
class COval :public CShape
{
private:
int mWidth;
int mHeight;
public:
COval :mWidth(100), mHeight(100) {} // constructor
int GetWidth() const
{
return mWidth;
}
int GetHeight() const
{
return mHeight;
}
void SetSize(int w, intg h)
{
mWidth = w;
mHeight = h;
}
}
Keywords
If CSquare is derived from CShape:
CShape is a base class of CSquare CShape is a super class of CSquare
CSquare extends CShape
Information Hiding
class A
{
public:
// Data and functions needed by clients (public interface)
protected:
// data and functions needed by derived classes
private:
// data and functions needed by class A only
}
What’s new to use here is the protected access modifier which is similar to the private modifier, the difference is that the class memeber declared as Protected are inaccessible outside the class but they can be accessed by any subclass (derived class) of that class.
Pointers
Week Three
What is Polymorphism in C++?
- It’s the ability of an object of one type to appear as and be used as another type
-
It occurs when there is a hierarchy of classes and they are related by inheritance
- Polymorphism is mainly divided into two types:
Compile Time Polymorphism
- Function Overloading (same name of function but different implementations)
- Operator Overloading (overloading “«” operator)
Runtime Polymorphism
- Virtual Functions ( occurs when a derived class has a defintionfor one of the memeber functions of the base class)
Virtual Functions
- A virtual function is a function in a base class that is declared using the keyword virtual
- Dynamic linkage or late binding
- By virtual the functionality of the function can be overridden by a derived class
Pure Virtual Functions
- Notes in Virtual Function Page
What is Upcasting?
Abstract Classes
- Abstract class is a class that cannot be insantiated, they’re only used as base class
How do you make an abstract class?
- Declare a member function to be a pure virtual
- Declare the constructor to be protected
- Inherit a pure virtual function without overriding
Week 4 Stuff
- const
- Constructor
- Destructors
- Assignment Operators
Group Together Classes