Revision - Inheritance and Separate Compilation - 30 Oct
Revision - Inheritance and Separate Compilation - 30 Oct
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:
private:
type name; // member variables
type name; // (data inside each object)
};
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;
}
String StudentClass::getName()
{
return name;
}
### application
int main()
{
StudentClass obj;
…..
….
return 0;
}
SEPARATING INTERFACE FROM
IMPLEMENTATION
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
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.
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;
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;
}
#include <iostream>
#include "Marks.h"
using namespace std;
int main()
{
Marks myMarks("Mary Msanzi", "123455", 50, 56, 65, 70);
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
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
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
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.
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;*/
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:
Include a statement to invoke the version of calcMark() in class Marks and display
Invoke the version of calcMark() defined in the derived class FinalMark and display