Lecture 2: Classes & Objects

Computer Programming

J Mwaura

Structured Programming

It's based on concept of calling procedure/funtions

A technique that consists of well-structured & separated modules

Follows logic of a program - it requires more discipline at the design and logical structuring stage

In procedural programming, we needed to only write an application program (the main function and some other functions) to use objects of built-in types

C++ has many predefined (built-in) types such as int, double, char, & bool

Example; C

Object-Oriented Programming

OOP brings together data & functions

Supports encapsulation, abstraction, inheritance, polymorphism

Includes data hiding feature

This model is based on real life entities

Example; C++, Java, Python, C#

Procedural/Structured

Object Oriented

Program section are called functionsProgram section are called objects
Follows top down approachFollows bottom up approach
No access specifierAccess specifiers like private, public, protected
Not easy to add new data & function is not easyEasy to add new data & function
Less secure - no data hidingSecure - data hiding
No overloadingAllows overloading
Function is more important than dataData is more important than function
Based on unreal worldBased on real world

Types & Instances

A type is a concept from which an instance is created or is an abstraction

An instance of that type is a concrete entity

  • clock is a type
  • clock in John's office, the clock in Sue's office, or the clock in a train station are instances of this type
  • circle is a type
  • circles with different radii as instances of that type
  • person is a type
  • John, Sue, and Michelle are instances of the type person

The relationship between a type & its instances is one-to-many

Attributes & Behavoirs

An attribute is a characteristic of an instance that we are interested in

For example, an employee is an instance, and the attributes are name, address, position, and salary

A behavior is an operation that we assume an instance can perform on itself

instances

Classes & Objects

In C++, a user-defined type can be created using a construct named class

An object is an instance of a class. This means that we use type and class

In OOP, attributes & behaviors of an object are represented as data members & member functions

Types of classes; Concrete, abstract & mixin

Data Member

A data member of an object is a variable whose value represents an attribute

  • i.e. Attributes of an object in OOP are simulated using data members
  • For example, the radius of a circle object can be represented by the value of a variable of type double

Data members in a class must not depend on each other

members

Member Function

In OOP, a function is an entity that can do something

A member function in OOP is a function that simulates one of the behaviors of an object

  • For example, we can write a function that allows a circle to give its radius, area, & perimeter
functions

Classes

In C++ OOP, Programmers can create new types, using classes

To write object-oriented programs, we need to create a class, as a type, and then instantiate objects as instances of that type

OOP requires 3 sections;

  • the class definition,
  • the member function definition, and
  • the application (which uses the objects created from the class)

Classes

  1. Class definition - declare the data members and member functions
  2. Member function definition - define all member functions
  3. Application - instantiate objects and apply the member function to those objects

C++ program in object-oriented paradigm

classes

Classes & Objects

Access Modifiers

Access modifier determines how a class can be accessed

When there is no access modifier for a member, it is private by default

modifiers

Access Modifiers for Data Members

A private data member is visible, but it cannot be accessed except through a member function

The data members of a class are normally set to private

modifiers

Access Modifiers for Member Functions

To operate on the data members, the application must use member functions, which means that the declaration of member functions usually must be set to public

The instance member functions of a class are normally set to public

Structs

C language uses structs

A struct in the C++ language is actually a class with one difference;

  • In a struct, all members are public by default; in a class, all members are private by default

We can always create a class to simulate a struct as shown below

struct{string first; char middle; string last; };

class{public: string first; char middle; string last; };

Constructors & Destructors

In OOP, an instance is an object that encapsulates the data members defined in the class definition

A constructor is a special member function that creates and initializes an object

A destructor is a special member function that cleans and destroys an object

modifiers

Constructors

A constructor has 2 characteristics;

  • It does not have a return value,
  • Its name is the same as the name of the class

Constructor overload - is having more than one constructors in a class

Types of constructors in a class

  • Parameter constructors - can be overloaded for a class
  • Default constructors - cannot be overloaded for a class
  • Copy constructors- cannot be overloaded for a class

Constructors Declaration

Parameter constructors

Supports overload, which means that we can have several parameter constructors each with a different signature

The advantage of the parameter constructor is that we can initialize the data members of each object with a specific value

For example; the radius of one circle can be initialized to 3.1, another one to 4.6


Bitbucket: Paramater Constructor


Home: Paramater Constructor

Default constructors

A constructor with no parameters

It is used to create objects with each data member for all objects set to some literal values or default values

No overload because it has no parameter list and therefore is not eligible for overloading


Bitbucket: Default Constructor


Home: Default Constructor

Copy constructors

Copies the data member values of the given object to the new object just created

Has only one parameter that receives source object by reference

The const modifier guarantees that the pass-by-reference cannot change the source object

No overload because the parameter list is fixed and we cannot have an alternative form


Bitbucket: Copy Constructor


Home: Copy Constructor

Destructors

A destructor has two special characteristics;

  • The name of the destructor is the name of the class preceded by a tilde symbol (~), a tilde
  • No return value (not even void) because it returns nothing.

A destructor is guaranteed to be automatically called and executed by the system when the object instantiated from the class goes out of scope

No overloads

Destructors Declaration & Definition

Constructors & Destructors

modifiers
modifiers

Instance Members

When designing a class, we can have two groups of members;

  • instance members & class members

An instance data members is normally private and can be accessed only through instance member functions

  • Instance data members are encapsulated in objects

An instance member function of a class must be public to be accessed from outside the class

Member selector operators;

  • Operator (.) - object.member e.g. (*this).radius
  • Operator (->) - pointer -> member e.g. this -> radius

Static Data Members

A class type can also have two types of members;

  • instance members
  • static members

A static data member is a data member that belongs to all instances; it also belongs to the class itself

static

Static Function Members

A static member function enables access to the corresponding static data member when an instance wants to access it or when the application needs to access it

A static member function cannot be used to access instance data members because it has no this pointer parameter. i.e. A static member function has no host object because it is not associated with any instance

static

To Wrap up - OOP

C++ as an OOP language, has normally 3 code sections;

  • Class definition (interface file)
  • Member function definition (implementation file)
  • Application (application file)
files

Interface file

Interface file contains the class definition i.e.,

  • data member declarations
  • member function declarations

It gives the general picture of the class to be used by the other files; i.e., it defines the type that is used by the other two files

The name of this file is normally the name of the class with an h extension, such as circle.h. The letter h designates it as a header file

Implementation file

Implementation file contains the definition of member functions

It is the code for all member function declarations given in the interface file

The name of this file is normally the name of the class with a cpp extension, such as circle.cpp, although the extension may vary in different C++ environments

Application file

Application file includes the main function that is used to instantiate objects and let each object perform operations on themselves

The application file must also have the extension cpp, but the name of the file is usually chosen by the user

The name we use is app.cpp, although the extension may vary in different C++ environments

Compilation

After creating 3 separate files, we must compile them to create an executable file. In C++, the process is referred to as separate compilation

compilation

End of Lecture 2

Computer Programming

That's it!

Queries about this Lesson, please send them to: jmwaura.uni@gmail.com

*References*

  • C++ Programming - An Object-Oriented Approach, 2019 Behrouz, Richard et.al
  • Accelerated C++ - Practical Programming by Example, 2000 Andrew, Barbara et.al
Courtesy of
Computer Programming