0% found this document useful (0 votes)
3 views51 pages

Classes

The document discusses the concepts of classes and abstraction in programming, emphasizing the importance of abstract data types and their separation from implementation. It covers class definitions, access specifiers, constructors, destructors, and operator overloading, along with the differences between classes and structs. Additionally, it introduces the concepts of upcasting and downcasting in the context of object-oriented programming.

Uploaded by

thezsk45
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)
3 views51 pages

Classes

The document discusses the concepts of classes and abstraction in programming, emphasizing the importance of abstract data types and their separation from implementation. It covers class definitions, access specifiers, constructors, destructors, and operator overloading, along with the differences between classes and structs. Additionally, it introduces the concepts of upcasting and downcasting in the context of object-oriented programming.

Uploaded by

thezsk45
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/ 51

Classes & Abstraction

Purpose of Program

 Quality

 Maintenance
ABSTRACTION vs ENACPSULATION
SPECIFICATION & INTERFACE
Data type
Objects

Operations
ABSTRACT DATA TYPE

• abstract data type is organized in such a manner that


specifications are kept separate from representation of
objects and implementation of the operations
• It has a set interface
• Abstract Data type can only be accessed by a program via
the interface
• Changing the implementation doesn’t change the program
(provided the interface has not changed)

Interface Implementation Interface

ADT
Program
Classes: Definitions

 A user defined data type aka abstract data type


 A special data type with
 Data members
 Behavior
 Access Specifiers
Class Declaration
Member Access Specifier

 An access specifier is one of the following three keywords:


 private,
 Public
 protected.
 These specifiers modify the access rights for the members that follow them:
 private members are accessible only from within other members of the same class (or from their "
 protected members are accessible from other members of the same class (or from their "friends"
but also from members of their derived classes.
 public members are accessible from anywhere where the object is visible.
Class Declaration
Class clockType
UML Class Diagram

- Private
# protected
+ public
Object Declaration & Memory
Allocation
 Memory is allocated only when the object is declared.
 Object aka instance
 Compute memory required for the class clockType.
Class Object Diagram
Accessing Class Members & what can
you access
Can & CanNOT

What can you do with What can you not do


classes with classes
Scope

 What is the scope of class objects?


Functions & Classes

 Functions
 Behavior
 Interface
 BUT IT IS STILL ONE TASK PER FUNCTION ONLY
Implementation of Functions
Constructor

 Properties
 Why would you need a constructor in the first place?
 Types of constructors
 Default constructor
 Parameterized constructor
 Fully parameteized constructor
 Constructor with default parameters
 Copy constructor
Default Parameters in Constructors
Special copy constructor

 When declaring a class object, you can initialize it by using the value of
an existing object of the same type.
Using copy constructors
Invoking constructor
Note regarding constructors

 Which constructors to implement?

 Why copy constructor is required?


Exercise: Draw UML class diagram &
object diagram
Exercise: Declare Objects
Array of objects

 clockType arrivalTimeEmp[100];
Class with pointers
Using assignment operator
shallow copy
Write copy constructor to achieve this
Destructor

 If only primitive data types, leave empty body


clockType::~clockType()
{
}
 If class contains a pointer
ptrMemberVarType::~ptrMemberVarType()
{
delete [] p;

}
Struct vs Classes

 Differentiate between classes and structs?


Abstraction in program

 “clockType.h”
 Containing declaration of class and its memberlist
 “clockType.cpp”
 Containing the definition of the functions
 Main
 Containing the main part where the objects are declared and the interfaces re
used
Upcasting and downcasting
Upcasting

 Upcasting is a process of treating a pointer or a reference of derived


class object as a base class pointer. You do not need to upcast manually.
You just need to assign derived class pointer (or reference) to base class
pointer

 When you use upcasting, the object is not changing. Nevertheless, when
you upcast an object, you will be able to access only member functions
and data members that are defined in the base class:
Upcasting

 One of the biggest advantage of upcasting is the capability of writing


generic functions for all the classes that are derived from the same base
class.

 his function will work with all the classes that are derived from the
Employee class. When you call it with objects of type Manager and
Person, they will be automatically upcasted to Employee class:
Pointers to class
some pointer and class operators
(*, &, ., ->, [ ])
Operator Overloading
Operator Overloading: format, MUST
DOs & Restrictions
clockType
Overload the following operators

 Assignment (=)
 Copies the values of one object of clockType into another
 Not equal to (!=)
 Return true if both objects are not equal
 Difference (-)
 Computes a valid difference between two objects
 Addition (+)
 Adds the two times of clocks and return a valid object of clock
 Hash (#)
 validates if the given clock object is valid or not.
Streams overloading: cin, cout

You might also like