Introduction To C Part 3
Introduction To C Part 3
Defining Classes
Class inheritance
Public, private, and protected access
Virtual functions
A first C++ class
Open project Basic_Rectangle.
A C++ class consists of 2 files: a header file (.h) and a source file (.cpp)
The header file contains the definitions for the types and names of members, methods, and
how the class relates to other classes (if it does).
The source file contains the code that implements the functionality of the class
An IDE is very useful for setting up code that follows patterns and configuring the build
system to compile them.
keyword
Curly brace
Access
control
Curly brace
and a
semi-colon.
Default declared methods
Rectangle();
A constructor. Called when an object of this class is
created.
~Rectangle();
A destructor. Called when an object of this class is
removed from memory, i.e. destroyed.
Ignore the virtual keyword for now.
Rectangle.cpp
Header file included
Member variables can be accessed as though they were passed to the method.
Methods can also call each other.
Fill in the Area() method and then write your own ScaledArea(). Don’t forget to compile!
Using the new class
These include:
Constructors – called when an object is created. Can have many defined per class.
return 0;
}
Default values class Rectangle {
public:
Rectangle();
Rectangle(const float width,
C++11 added the ability to define default const float length) ;
values in headers in an intuitive way.
Rectangle(const Rectangle& orig);
virtual ~Rectangle();
Pre-C++11 default values would have been
coded into constructors. float m_length = 0.0 ;
float m_width = 0.0 ;
If members with default values get their value
set in constructor than the default value is float Area() ;
float ScaledArea(const float scale);
ignored.
i.e. no “double setting” of the value. private:
};
Default constructors and destructors
The two methods created by Eclipse automatically class Foo {
are explicit versions of the default C++ constructors public:
and destructors. Foo() = delete ;
// Another constructor
// must be defined!
Every class has them – if you don’t define them then Foo(int x) ;
empty ones that do nothing will be created for you by };
the compiler. class Bar {
If you really don’t want the default constructor you can public:
delete it with the delete keyword. Bar() = default ;
Also in the header file you can use the default keyword };
if you like to be clear that you are using the default.
Custom constructors and destructors
You must define your own constructor when you want to initialize an
object with arguments.
House object
Destructors
Example:
Defining Classes
Class inheritance
Public, private, and protected access
Virtual functions
Inheritance
Inheritance is the ability to form a
hierarchy of classes where they
share common members and Molecule
methods.
Helps with: code re-use, consistent
programming, program organization
Inorganic Organic
This is a powerful concept!
Mineral Protein
Inheritance Superclass Base Class
The class being derived from is referred
to as the base, parent, or super class.
Mineral Protein
Inheritance in Action
cout is an object of the class ostream. It How might an object of class ofstream or
is a write-only series of characters that ostringstream be used if we want to write
prints to the terminal. characters to a file or to a string?
For ofstream and ofstringstream the << operator is inherited from ostream
and behaves the same way for each from the programmer’s point of view.
The ofstream class adds a constructor to open a file and a close() method.
ofstringstream adds a method to retrieve the underlying string, str()
Defining Classes
Class inheritance
Public, private, and protected access
Virtual functions
“There are only two things wrong with C++: The initial concept
and the implementation.”
Public, protected, private – Bertrand Meyer (inventor of the Eiffel OOP language)
class Rectangle
{
public:
Public and private were added by Rectangle();
Rectangle(float width, float length) ;
NetBeans to the Rectangle class. virtual ~Rectangle();
float m_width ;
These are used to control access float m_length ;
private:
};
C++ Access Control and Inheritance
Access public protected private
Same class Yes Yes Yes
Subclass Yes Yes No
Outside classes Yes No No
Inheritance
class Super {
public: class Sub : public Super {
int i; // in methods, could access
protected: // i and j from Parent only.
int j ; };
private:
int k ; Outside code
};
Sub myobj ;
Myobj.i = 10 ; // public - ok
Myobj.j = 3 ; // protected - Compiler error
Myobj.k = 1 ; // private - Compiler error
class A class B : public A
Inheritance public public A public
private private
Square::~Square()
class Square : public Rectangle {}
{
public:
Square();
virtual ~Square();
protected:
Note that subclasses are free to add any number
private: of new methods or members, they are not limited
}; to those in the superclass.
#endif // SQUARE_H
A square is, of course, just a rectangle with equal length and width.
The area can be calculated the same way as a rectangle.
Our Square class therefore needs just one value to initialize it and it can
re-use the Rectangle.Area() method for its area.
private: This is unsatisfying – while there is nothing wrong with this it’s
}; not the OOP way to do things.
#endif // SQUARE_H
Why re-code the perfectly good constructor in Rectangle?
The delegating constructor class class_c {
public:
int max;
int min;
int middle;
Square::Square(float length) :
Reference: Rectangle(length,length)
{
https://msdn.microsoft.com/en-us/library/dn387583.aspx
// other code could go here.
}
Solution 2
#ifndef SQUARE_H #include "Square.h"
#define SQUARE_H
Square::Square(float length) :
#include "Rectangle.h" Rectangle(length, length) {}
class Square : public Rectangle Square can directly call its superclass constructor and let the
{ Rectangle constructor make the assignment to m_width and
public:
m_length.
Square(float width);
virtual ~Square();
This saves typing, time, and reduces the chance of adding
protected: bugs to your code.
The more complex your code, the more compelling this statement
private:
is.
};
#endif // SQUARE_H Code re-use is one of the prime reasons to use OOP.
Trying it out in main()
#include <iostream>
What happens behind the scenes
when this is compiled…. using namespace std;
int main()
Square class {
does not
implement Area() Square sQ(4) ;
so compiler looks
to superclass
// Uses the Rectangle Area() method!
cout << sQ.Area() << endl ;
Inserts call to
Rectangle.Area()
method in
compiled code.
More on Destructors
When a subclass object is
removed from memory, its
Square object is
destructor is called as it is for any removed from
object. memory