Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding Virtual Functions and Inheritance in C++: A Guide to Creating a Square Class, Slides of Construction

A step-by-step guide on how to create a Square class that inherits from the Rectangle class in C++. It covers the concepts of virtual functions, member initialization lists, and delegating constructors. Learn how to initialize the Square class with a single value and access the public members and methods in its superclass.

What you will learn

  • What is virtual function inheritance in C++?
  • How does a subclass access private and protected members of its superclass?
  • How do you create a Square class that inherits from a Rectangle class?
  • What is a delegating constructor in C++?
  • What is the difference between single and multiple inheritance in C++?

Typology: Slides

2021/2022

Uploaded on 09/12/2022

lumidee
lumidee 🇺🇸

4.4

(47)

364 documents

1 / 48

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to C++: Part 3
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30

Partial preview of the text

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 NewClass

 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

SourceImplement 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 ;