0% found this document useful (0 votes)
19 views38 pages

Revision - Inheritance and Separate Compilation - 30 Oct

Uploaded by

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

Revision - Inheritance and Separate Compilation - 30 Oct

Uploaded by

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

ABSTRACT DATA TYPES (ADT)

AND
INHERITANCE
DATA ABSTRACTION, CLASSES, AND
ABSTRACT DATA TYPES (ADT)

An abstract data type is one where the programmers who use the type do not
have access to the details of how the values and operations are implemented.
Type defined in terms of its data items and associated operations, not its
implementation.
Examples:
 When driving a car, the driver is exposed to only the interface like accelerator,
steering wheel, etc.
 Operations are performed based on this interface.
 The driver doesn’t have control of how these functionalities are implemented.
 Implementation details are hidden.
An Abstract Data Type (ADT) separates the class definition (interface) and the class
implementation into separate files that are compiled separately and then linked together when the
program is run.
Tips for separate compilation with multiple files:

 The .h and the .cpp files should be ‘added’ to the project.


 All files must be in the same directory or folder as the project file.
 The .cpp files must contain the preprocessor directive to include the .h files.
e.g. #include "Movie.h"
Note that all the files (Movie.h, Movie.cpp and main.cpp) must be in a project in the same
folder.
INTERFACE VS. CODE
 In C++, when writing classes you must understand separation of:
 interface: Declarations of functions, classes, members, etc.
 implementation: Definitions of how the above are implemented.

 C++ implements this separation using two kinds of code files:


 .h: A "header" file containing only interface (declarations).
 .cpp: A "source" file containing definitions.
 For instance, when you define a new class Student, you create:
 Student.h (containing the declarations) and
 Student.cpp (containing the definitions).

 The content of the .h file is "#included" inside .cpp file.


 Makes the class aware of declarations of code implemented elsewhere.
 At compilation, all definitions are linked together into an executable file.
A CLASS DECLARATION
class ClassName { // in ClassName.h
public:
ClassName(parameters); // constructor

returnType name(parameters); // member functions


returnType name(parameters); // (behaviour inside
returnType name(parameters); // each object)

private:
type name; // member variables
type name; // (data inside each object)
};

IMPORTANT: must put a semicolon at the end of the class declaration


IMPLEMENTING AN ABSTRACT DATA
TYPE WITH A CLASS
Member access specifiers
Classes can limit the access to their member functions and
data
The three types of access a class can grant are:
 Public — Accessible wherever the program has access to an object of the
class
 Private — Accessible only to member functions of the class
 Protected — Similar to private
— also visible within derived classes

Constructor
Special member function that initializes the data members
of a class object
Cannot return values
Has the same name as the class
### Declaration
Class StudentClass
{ public:
Constructor – ALWAYS same name as
StudentClass(); class.
void setStudentID(string ID); Mutators
void setName(string nam);
void setYear(int yr);
void setGpa(double G_pa);
int getStudentID() const; Accessors
string getName() const;
double getGpa() const;

private:
Private member variables – cannot be
int studentID; DIRECTLY accessed outside this class
string name;
int year;
double gpa;
};
 ### implementation:
StudentClass::StudentClass()
{
studentID = 0;
name = “ “;
year = 0;
gpa = 0;
}

void StudentClass::setName(string nam)


{
name = nam;
}

String StudentClass::getName()
{
return name;
}

### application
int main()
{
StudentClass obj;
…..
….
return 0;
}
SEPARATING INTERFACE FROM
IMPLEMENTATION

Makes it easier to modify programs

N.B.
Header file
Contains class definitions and function prototypes

Source-code file
Contains member function definitions
CONVERTING THE
STUDENTCLASS INTO
SEPARATE FILES
 Move declaration part into a header file:
 StudentClass.h file

 Move the implementation part into a cpp file


 StudentClass.cpp

 Keep the application inside the main.cpp


StudentClass.h file
### Declaration
#ifndef STUDENTCLASS_H
#define STUDENTCLASS_H
#include <iostream>
#include <string>
using namespace std;

Class StudentClass
{ public:
StudentClass();
void setStudentID(string ID);
void setName(string nam);
void setYear(int yr);
void setGpa(double G_pa);
int getStudentID() const;
string getName() const;
double getGpa() const;

private:
int studentID;
string name;
int year;
double gpa;
};
#endif
 StudentClass.cpp

 ### implementation:
#include StudentClass.h
#include <iostream>
#include <string>
using namespace std;

StudentClass::StudentClass()
{
studentID = 0;
name = “ “;
year = 0;
gpa = 0;
}
void StudentClass::setName(string nam)
{
name = nam;
}
String StudentClass::getName() const
{
return name;
}
Main.cpp
### application

#include <iostream>
#include <string>
#include "StudentClass.h"
using namespace std;

int main()
{
StudentClass obj;
obj.setName("First Name");
cout<< obj.getName() <<endl;
obj.setName("Changed name");
cout<< obj.getName() <<endl;
return 0;
}
N.B.

 In the exam you are expected to use separate compilation to create


ADTs and use them in application programs.

 If a question asks you to define a class, and you provide the


implementation of the class instead of the class definition, or vice versa,
you will not receive any marks.
Important OOP concepts:
1. Inheritance: mechanism that allows a class to share the properties and methods of another
class, but also extend the functionality provided by that class.

Benefits:
 Code can be reused.
 Code has been tried and tested.
 More reliable code.
Important OOP concepts:
2. Encapsulation:combining the data (represented by the member variables) with the
member functions that operate on that data, into a single unit. The purpose: information hiding i.e.
hiding the implementation details from the class user.

Benefits:

 Hiding Data: Users will have no idea how classes are being implemented or stored. All that
users will know is that values are being passed and initialized.
 More Flexibility: Enables you to set variables as read or write-only. You can use mutators such
as setName(), or setAge() to set member variables as write-only if you omit the accessors like
getName(), getAge() in the class.
 Easy to Reuse: With encapsulation it's easy to change and adapt to new requirements.
Important OOP concepts:
3. Abstraction: implementation details are hidden from the class user to reduce
complexity – refers to how an object and its behaviours are presented to the class user.

Benefits:
 Reduces complexity
 Allows class user to interact more efficiently with the program.
INHERITANCE EXAMPLE
Consider the class definition below:

Note: The class definition is also called the class specification or the interface for the class.
class Marks
{
public:
Marks();
Marks (string name, string number, int asg1, int asg2,
int asg3, double test);
double calcMark() const;
private:
string stdtName;
string stdNumber;
Int assignments[3];
double testMark;

 Implement the class Marks and test it in a driver program.


INHERITANCE EXAMPLE
 To implement the class:

STEP 1: Use separate compilation and create a header file that contains the class definition (Marks.h).

#include <string>
using namespace std;
class Marks
{
public:
Marks();
Marks(string name, string number, int asg1, int asg2,
int asg3, double test);
string getStdtName()const;
string getStdNumber()const;
double getTestMark()const;
int getAsgn1()const;
int getAsgn2()const;
int getAsgn3()const;
double calcMark() const;
INHERITANCE EXAMPLE
STEP 1: header file (continued)

private:
string stdtName;
string stdNumber;
int assignments[3];
double testMark;
};
#endif // MARKS_H
INHERITANCE EXAMPLE
 To implement the class:

STEP 2: create an implementation file for the class in which the definitions for the member functions of the
class are provided (Marks.cpp).

Marks.cpp
#include "Marks.h"
#include <iostream>
#include <string>

//default constructor
Marks::Marks():stdtName(""),stdNumber(""),assignments{0,0,0},
testMark(0)
{
//body empty
}
INHERITANCE EXAMPLE
Marks.cpp (continued)
//overloaded constructor
Marks::Marks (string name, string number, int asg1, int asg2, int asg3,
double test)
{
stdtName = name;
stdNumber = number;
assignments[0] = asg1;
assignments[1] = asg2;
assignments[2] = asg3;
testMark = test;
}
INHERITANCE EXAMPLE
Marks.cpp (continued)
string Marks::getStdtName() const // accessor
{
return stdtName;
}

string Marks::getStdNumber() const // accessor


{
return stdNumber;
}
INHERITANCE EXAMPLE
Marks.cpp (continued)
int Marks::getAsgn1() const// accessor
{
return assignments[0];
}
int Marks::getAsgn2() const// accessor
{
return assignments[1];
}
int Marks::getAsgn3() const// accessor
{
return assignments[2];
}
INHERITANCE EXAMPLE
Marks.cpp (continued)
double Marks::getTestMark() const //accessor
{
return testMark;
}

double Marks::calcMark() const


{
int total = 0;
for (int i = 0; i <= 2; i++)
total += assignments[i];
total += testMark;
return total/4;
}
INHERITANCE EXAMPLE
 The class is tested in a driver program i.e. an application program.

 By default, the main.cpp is the application program for a project.

STEP 3: create a driver program (main.cpp).

#include <iostream>
#include "Marks.h"
using namespace std;
int main()
{
Marks myMarks("Mary Msanzi", "123455", 50, 56, 65, 70);

cout << "The mark for " << myMarks.getStdtName()


<< " with student number " << myMarks.getStdNumber()
<< " is " << myMarks.calcMark() << endl;
return 0;
}
INHERITANCE EXAMPLE
Derive a class FinalMark from class Marks.

This class has an additional member variable examMark that holds the examination mark.

FinalMark.h

#ifndef FINALMARK_H
#define FINALMARK_H
#include <iostream>
#include <string>
#include "Marks.h"
using namespace std;
INHERITANCE EXAMPLE
FinalMark.h (continued)
class FinalMark:public Marks
{
public:
FinalMark();
FinalMark(string name, string number, int asg1, int asg2, int asg3,
double test, double exam);
int calcMark( ) const;
double getExamMark();
private:
double examMark;
};
#endif // FINALMARK_H
INHERITANCE EXAMPLE
 Implement the overloaded constructor for the class FinalMark by invoking the base class constructor.

FinalMark.cpp

//default constructor for class FinalMark invoking base class //constructor


FinalMark::FinalMark(): Marks(), examMark(0)
{
// empty
}

//overloaded constructor for class FinalMark invoking base class //constructor


FinalMark::FinalMark (string name, string number, int asg1, int asg2,
int asg3, double test, double exam):
Marks (name, number, asg1, asg2, asg3, test),
examMark(exam)
{
//empty
}
INHERITANCE EXAMPLE
Implement the member function calcMark() for the derived class FinalMark.

The test mark contributes 20% to the final mark, the average of the three assignments

contributes 10% and the examination mark contributes 70% to the final mark.

In the class FinalMark, the member function calcMark() should override the version in

Marks.

The member function should return the final mark as an integer. We use casting to

convert the result of the calculation to an integer:

N.B.: member variables testMark and assignments are private to the class Marks and cannot be accessed

directly.
INHERITANCE EXAMPLE
 Method 1:

Use the accessors defined in Marks to access the member variables testMark and assignments[3]
to implement calcMark() in the class FinalMark.
N.B.: calcMark() is a const function, the accessors also need to be declared const. It is always a
good idea to declare accessors const to prevent inadvertent changes to the member variables.
//Using accessors to access values of member variables

int FinalMark::calcMark() const


{
int finalMark = (int)(0.2 * getTestMark() + (0.1 * (getAsgn1() +
getAsgn2() + getAsgn3() )/3) + 0.7 * examMark);
return finalMark;
}
INHERITANCE EXAMPLE
 Method 2:

To directly access the member variables in the base class:

 Declare them to be protected instead of private.

 When the member variables are protected, they become accessible to the derived class FinalMark and

can be directly accessed by the member functions and friend functions of FinalMark.

 This allows access to the member variables without using accessors.

 Note: Declaring a member variable or member function as protected may cause unintentional errors

because the accessor functions no longer guard protected member variables against inadvertent errors. In
general, it is advisable NOT to use the protected qualifier.
INHERITANCE EXAMPLE
Marks.h (amended)
/* no longer needed – hence commented out
double getTestMark()const;
int getAsgn1()const;
int getAsgn2()const;
int getAsgn3()const;*/

double calcMark() const;


protected:
int assignments[3];
double testMark;

private:
string stdtName;
string stdNumber;

// int assignments[3];
// double testMark;
INHERITANCE EXAMPLE
Implementation of member function calcMark() in derived class FinalMark when member variables
assignments and testMark are protected in base class Marks:

//redefined calcMark in class FinalMark with protected member


//variables in class Marks, no need for accessors

int FinalMark::calcMark() const


{
int finalMark = (int)(0.2 * testMark + (0.1 * (assignments[1] +
assignments[2] + assignments[3] )/3) + 0.7 * examMark);
return finalMark;
}
INHERITANCE EXAMPLE
Test class FinalMark in a driver program:

 Include the instantiation: FinalMark myFinalMark;

 Include a statement to invoke the version of calcMark() in class Marks and display

the name and student number with the mark.

 Invoke the version of calcMark() defined in the derived class FinalMark and display

the name and student number with the mark.


INHERITANCE EXAMPLE
Marks.h (amended)
Main.cpp
#include <iostream>
#include <string>
#include "FinalMark.h"
#include "Marks.h"
using namespace std;
int main()
{
FinalMark myMark("Mary Msanzi", "123455", 50, 56, 65, 70, 63);
//to call version of calcMark in class Marks
cout << "The year mark for " << myMark.getStdtName()
<< " with student number " << myMark.getStdNumber()
INHERITANCE EXAMPLE
<< " is " << myMark.Marks::calcMark() << endl;

cout << "The exam mark for " << myMark.getStdtName()


<< " with student number " << myMark.getStdNumber()
<< " is " << myMark.getExamMark() << endl;

//call version of calcMark in class FinalMark


cout << "The final mark for " << myMark.getStdtName()
<< " with student number " << myMark.getStdNumber()
<< " is " << myMark.calcMark() << endl;
return 0;
}
Road map to creating classes

See document: A path to link the concepts around objects.

You might also like