Download Understanding Virtual Functions and Inheritance in C++: A Guide to Creating a Square Class and more Slides Construction in PDF only on Docsity!
- Introduction to C++: Part
Tutorial Outline: Part 3
Defining Classes
Class inheritance
Public, private, and protected access
Virtual functions
Using Eclipse
An IDE is very useful for setting up code that follows patterns and configuring the build
system to compile them.
This saves time and effort for the programmer.
Right-click on the Basic_Rectangle project and choose New Class
Give it the name Rectangle and click the Finish button. Open the new files Rectangle.h and Rectangle.cpp
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 Class_name:: pattern indicates the method declared in the header is being implemented in code here. Methods are otherwise regular functions with arguments () and matched curly braces {}.
Encapsulation
Bundling the data and area calculation for a rectangle into a
single class is an example of the concept of encapsulation.
The code for the two methods is needed
Right-click in the Rectangle.h
window and choose
SourceImplement Methods
Fill in the methods
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!
Step 1: add some comments.
Step 2: add some code.
Using the new class
Open Basic_Rectangle.cpp
Add an include statement for
the new Rectangle.h
Create a Rectangle object
and call its methods.
We’ll do this together…
Construction and Destruction
The constructor is called when an
object is created.
This is used to initialize an object:
Load values into member variables
Open files
Connect to hardware, databases,
networks, etc.
The destructor is called when an
object goes out of scope.
Example:
Object c1 is created when the
program reaches the first line of
the function, and destroyed when
the program leaves the function.
void function () {
ClassOne c1 ;
When an object is instantiated…
The rT object is created in memory.
When it is created its constructor is called to
do any necessary initialization.
The constructor can take any number of
arguments like any other function but it
cannot return any values.
What if there are multiple constructors?
The compiler follows standard function overload rules. Note the constructor
has no return type!
Member Initialization Lists
Syntax:
MyClass (int A , OtherClass & B , float C ): m_A ( A ), m_B ( B ), m_C ( C ) { /* other code can go here */ }
Colon goes here
Members assigned
and separated with
commas. The order
doesn’t matter.
Additional code can be
added in the code
block.
And now use both constructors
Both constructors are now used.
The new constructor initializes the
values when the object is created.
Constructors are used to:
Initialize members
Open files
Connect to databases
Etc.
#include
using namespace std ;
#include "Rectangle.h“
int main (int argc, char** argv)
Rectangle rT ;
rT. m_width = 1.0 ;
rT. m_length = 2.0 ;
cout << rT. Area () << endl ;
Rectangle rT_2 ( 2.0 , 2.0 ) ;
cout << rT_2. Area () << endl ;
return 0 ;