0% found this document useful (0 votes)
2 views

Classes and Objects

The document provides an overview of object-oriented programming (OOP) concepts, including abstract data types, classes, objects, member functions, constructors, and destructors. It emphasizes the importance of encapsulation, data hiding, and the separation of class specification and implementation. Additionally, it discusses the advantages of OOP over procedural programming, such as improved maintainability and reduced complexity.

Uploaded by

Santosh Kumar
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)
2 views

Classes and Objects

The document provides an overview of object-oriented programming (OOP) concepts, including abstract data types, classes, objects, member functions, constructors, and destructors. It emphasizes the importance of encapsulation, data hiding, and the separation of class specification and implementation. Additionally, it discusses the advantages of OOP over procedural programming, such as improved maintainability and reduced complexity.

Uploaded by

Santosh Kumar
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/ 47

TOPICS

Abstract Data Types


Object-Oriented Programming
Introduction to Classes
Creating and Using Objects
Defining Member Functions
Constructors
Destructors
Private Member Functions
TOPICS (CONTINUED)
Passing Objects to Functions
Object Composition
Separating Class Specification,
Implementation, and Client Code
ABSTRACT DATA TYPES
 Are programmer-created data types that
specify
 the legal values that can be stored
 the operations that can be done on the
values
 The user of an abstract data type (ADT)
does not need to know any
implementation details (e.g., how the
data is stored or how the operations
on it are carried out)
ABSTRACTION IN
SOFTWARE DEVELOPMENT
 Abstraction allows a programmer to
design a solution to a problem and to
use data items without concern for
how the data items are implemented
 This has already been encountered in
the book:
 Touse the pow function, you need to
know what inputs it expects and what
kind of results it produces
 You do not need to know how it works
ABSTRACTION AND DATA
TYPES
 Abstraction: a definition that captures
general characteristics without details
ex: An abstract triangle is a 3-sided polygon.
A specific triangle may be scalene,
isosceles, or equilateral
 Data Type: defines the kind of values
that can be stored and the operations
that can be performed on the values
OBJECT-ORIENTED
PROGRAMMING
 Procedural programming focuses on the
processes/ actions that occur in a program.
Data and functions are separate and distinct.
 Object-oriented programming is based on
objects that encapsulate the data and the
functions that operate with and on the data.
LIMITATIONS OF
PROCEDURAL PROGRAMMING

 Use of global data may allow data


corruption
 Programs are often based on complex
function hierarchies
 difficultto understand and maintain
 difficult to modify and extend
 easy to break
OBJECT-ORIENTED PROGRAMMING
TERMINOLOGY
 class: similar to a struct

 Allows
bundling of related variables (member
data) and the functions that operate on them
(member functions)
 Describes the properties that all instances of the
class will have
 object: an instance of a class, in the same
way that a variable can be an instance of a
struct
OBJECT-ORIENTED PROGRAMMING
TERMINOLOGY

 attributes: the data items of an object, stored


in member variables
 member functions (methods): procedures/
functions that act on the attributes of the class
MORE OBJECT-ORIENTED
PROGRAMMING TERMINOLOGY
 data hiding: restricting data access to certain
members of an object.
 Protection –provide a layer of protection against
inadvertent or deliberate data corruption
 Need-to-know –the programmer needn’t worry about
implementation details when writing “client” code
 encapsulation: the bundling of an object’s
data and procedures into a single entity
OBJECT EXAMPLE
Square

Member variables (attributes)

int side;

Member functions

void setSide(int s)

int getSide()

Square object’s data item: side

Square object’s functions: setSide - set the size of the side of the

square, getSide - return the size of the side of the square


INTRODUCTION TO
CLASSES
 Class: a programmer-defined data type
used to define objects
 It is a pattern for creating objects
ex:
string fName, lName;

This creates two objects of the string


class
INTRODUCTION TO
CLASSES
 A class declaration describes the member
variables and member functions that its
objects will have
 It is a pattern for creating objects
 declaration format:
class className
{
declaration;
declaration;
};
Note the

required ;
ACCESS SPECIFIERS
 Used to control access to members of the
class.
 Each member is declared to be either
public: can be accessed by functions
outside of the class
or
private: can only be called by or accessed
by functions that are members of
the class
CLASS EXAMPLE
class Square
{
private:
Access specifiers int side;
public:
void setSide(int
s)
{ side = s; }
int getSide()
{ return side; }
};
MORE ON ACCESS
SPECIFIERS

 Can be listed in any order in a class


 Can appear multiple times in a class
 If not specified, the default is private
CREATING AND USING
OBJECTS
 An object is an instance of a class
 It is defined just like other variables
Square sq1, sq2;
 It can access members using dot
operator
sq1.setSide(5);
cout << sq1.getSide();
DEFINING MEMBER
FUNCTIONS
 Member functions are part of a class
declaration

 Place entire function definition inside the class


declaration,
or
 Place just the prototype inside the class
declaration and write the function
definition after the class
TYPES OF MEMBER
FUNCTIONS
 Acessor, get, getter function: uses
but does not modify a member variable
ex: getSide

 Mutator, set, setter function:


modifies a member variable
ex: setSide
DEFINING MEMBER FUNCTIONS
INSIDE THE CLASS DECLARATION
 Member functions defined inside the class
declaration are called inline functions
 Only very short functions, like the one below,
should be inline functions
int getSide()
{ return side; }
INLINE MEMBER FUNCTION
EXAMPLE

class Square
{
private:
int side;
public:
void setSide(int s)
inline functions { side = s; }
int getSide()
{ return side; }
};
DEFINING MEMBER FUNCTIONS AFTER
THE CLASS DECLARATION *PREFERRED WAY

 Put a function prototype in the class


declaration
 In the function definition, precede the
function name with the class name and
scope resolution operator (::)
int Square::getSide()
{
return side;
}
CONVENTIONS AND A
SUGGESTION
Conventions:
 Member variables are usually private
 Accessor and mutator functions are usually
public
 Use ‘get’ in the name of accessor functions,
‘set’ in the name of mutator functions

Suggestion: If possible, use member variables


to calculate a value to be returned, as
opposed to storing the calculated value. This
minimizes the likelihood of stale data.
TRADEOFFS OF INLINE VS.
REGULAR MEMBER FUNCTIONS
 When a regular function is called,
control passes to the called function
 thecompiler stores return address of call,
allocates memory for local variables, etc.
 Code for an inline function is copied into
the program in place of the call when
the program is compiled
 larger executable program, but
 less function call overhead, hence faster
execution
DESIGN CONSIDERATIONS

 Class should be designed to provide methods


to store and retrieve data
 In general, I/O should be done by functions that
use class objects, rather than by class member
functions (Exceptions can occur, as with a
class designed to display a menu)
CONSTRUCTORS
 A constructor is a member function Is can be
used to initialize data members
 It must be a public member function
 It must be named the same as the class
 It must have no return type
 is automatically called when an object of the
class is created
CONSTRUCTOR – 2
EXAMPLES
Inline: Declaration
class Square
{
outside the class:
Square(int);
. . . //prototype
public: //in
Square(int class
s)
{ side = Square::Square(int s)
s; } {
. . . side = s;
}; }
ANOTHER DEFAULT CONSTRUCTOR
EXAMPLE
class Square
{
private: Has parameter
int side; but it has a default value

public:
Square(int s = 1) // default
{ side = s; } // constructor
// Other member
// functions go here
};
OVERLOADING CONSTRUCTORS
 A class can have more than 1 constructor
 Overloaded constructors in a class must have
different parameter lists
Square();
Square(int);
THE DEFAULT CONSTRUCTOR
 Constructors can have any number of
parameters, including none
 A default constructor is one that
takes no arguments either due to
 No parameters or
 All parameters have default values

 If a class has any programmer-defined


constructors, it should have a
programmer - defined default
constructor
DEFAULT CONSTRUCTOR
EXAMPLE
class Square
{
private: Has no
int side;
parameters

public:
Square() // default
{ side = 1; } // constructor
// Other member
// functions go here
};
ANOTHER DEFAULT CONSTRUCTOR EXAMPLE

class Square
{
private: Has parameter

int side; but it has a default

value
public:
Square(int s = 1) // default
{ side = s; } // constructor
// Other member
// functions go here
};
INVOKING A CONSTRUCTOR
 To create an object using the default
constructor, use no argument list and no ()
Square square1;
 To create an object using a constructor that has
parameters, include an argument list
Square square2(8);
DESTRUCTORS
 Are public member functions that
are automatically called when
objects are destroyed
 The destructor name is
~className, e.g., ~Square
 It has no return type
 It takes no arguments
 Only 1 destructor is allowed per
class
(i.e., it cannot be overloaded)
PRIVATE MEMBER
FUNCTIONS

 A private member function can only be


called by another member function of the
same class
 It is used for internal processing by the
class, not for use outside of the class
PASSING OBJECTS TO FUNCTIONS
 A class object can be passed as an
argument to a function.
 When it is passed by value, the
function makes a local copy of the
object. The original object in calling
environment is unaffected by actions
in the function.
 When passed by reference, the
function can use ‘set’ functions to
modify the object in the calling
environment.
NOTES ON PASSING
OBJECTS
 Using a value parameter for an object can slow
down a program and waste space

 Using a reference parameter speeds up the


program. It allows the function to modify data
in the parameter, which may not be desirable.
NOTES ON PASSING
OBJECTS
 To save space and time while protecting
parameter data that should not be changed,
use a const reference parameter
void showData(const Square &s)
// header
 In order for the showData function to call
Square member functions, those functions
must use const in their prototype and
header:
int Square::getSide() const;
RETURNING AN OBJECT FROM
A FUNCTION
 A function can return an object
Square initSquare(); // prototype
Square s1 = initSquare();// call
 The function must create an object
 for internal use
 to use with return statement
RETURNING AN OBJECT
EXAMPLE
Square initSquare()
{
Square s; // local object
int inputSize;
cout << "Enter the length of side: ";
cin >> inputSize;
s.setSide(inputSize);
return s;
}
SEPARATING CLASS SPECIFICATION,
IMPLEMENTATION, AND CLIENT CODE

Separating the class declaration, member


function definitions, and the program that uses
the class into separate files is considered good
design.
USING SEPARATE FILES
 Place the class declaration in a header file that
serves as the class specification file. Name the
file classname.h (for example, Square.h)
 Place the member function definitions in a class
implementation file. Name the file
classname.cpp (for example, Square.cpp)This
file should #include the class specification file.
 A client program (client code) that uses the
class must #include the class specification file
and be compiled and linked with the class
implementation file.
INCLUDE GUARD
 Is used to prevent a header file from being included
twice
 Format:
#ifndef symbol_name
#define symbol_name
. . . (normal contents of header file)
#endif
 symbol_name is usually the name of the header file, in
all capital letters:
#ifndef SQUARE_H
#define SQUARE_H
. . .
#endif
#PRAGMA ONCE
This instructor prefers to use
#pragma once
at the beginning of the file, because it is
too easy to forget the #endif
WHAT SHOULD BE DONE INSIDE
VS. OUTSIDE THE CLASS

 Class should be designed to provide functions


to store and retrieve data
 In general, input and output (I/O) should be
done by functions that use class objects, rather
than by class member functions
OBJECT COMPOSITION
 This occurs when an object is a member
variable of another object.
 It is often used to design complex
objects whose members are simpler
objects
 ex. (from book): Define a rectangle
class. Then, define a carpet class and
use a rectangle object as a member of a
carpet object.
OBJECT COMPOSITION,
CONT.

You might also like