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

COSC1020-Lecture18

COSC 1020 Computer Science I covers fundamental concepts of object-oriented programming, including abstract data types, classes, and member functions. Key topics include constructors, destructors, data encapsulation, and the distinction between public and private member functions. The course emphasizes the importance of abstraction and data hiding in software design, along with practical applications of these concepts in programming.

Uploaded by

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

COSC1020-Lecture18

COSC 1020 Computer Science I covers fundamental concepts of object-oriented programming, including abstract data types, classes, and member functions. Key topics include constructors, destructors, data encapsulation, and the distinction between public and private member functions. The course emphasizes the importance of abstraction and data hiding in software design, along with practical applications of these concepts in programming.

Uploaded by

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

COSC 1020 Computer Science I

COSC 1020 1
Topics
7.1 Abstract Data Types
7.2 Object-Oriented Programming
7.3 Introduction to Classes
7.4 Creating and Using Objects
7.5 Defining Member Functions
7.6 Constructors
7.7 Destructors
7.8 Private Member Functions
7.9 Passing Objects to Functions
7.10 Object Composition
7.11 Separating Class Specification,
Implementation, and Client Code
7.12 Structures
7.13 Introduction to
Object-Oriented Analysis and Design

7-2
7.1 Abstract Data Types

• Programmer-created data types that specify


– legal values that can be stored
– 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)
• This has already been encountered in the book:
– To use 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

7-3
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 it

7-4
7.2 Object-Oriented Programming

• Procedural programming uses variables to store


data, and focuses on the processes/ functions 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 on it.

7-5
Object-Oriented Programming Terminology

• object: software entity that combines data and


functions that act on the data in a single unit
• attributes: the data items of an object, stored in
member variables
• member functions (methods): procedures/ functions
that act on the attributes of the class
• data hiding: restricting access to certain members of
an object. The intent is to allow only member functions
to directly access and modify the object’s data
• encapsulation: the bundling of an object’s data and
procedures into a single entity

7-6
Object Example
UML Diagram class Declaration
Rectangle class Rectangle
{
- length : int private:
- width : int int length;
+ setLength( l : int ) : void int width;
+ setWidth( w : int ) : void
public:
+ getLength() const : int void setLength(int l);
+ getWidth() const : int void setWidth( int w);

int getLength() const;


Rectangle object’s data items: int getWidth() const;
length };
width

Rectangle object’s functions:


setLength - set the value of the rectangle's length
setWidth – set the value of the rectangle's width
getLength - return the value of the rectangle's length
getWidth – return the value of the rectangle's width
7-7
Why Hide Data?

• Protection – Member functions provide a layer


of protection against inadvertent or deliberate
data corruption
• Need-to-know – A programmer can use the
data via the provided member functions. As
long as the member functions return correct
information, the programmer need not worry
about implementation details.

7-8
7.3 Introduction to Classes

• Class: a programmer-defined data type used to


define objects
• It is a pattern for creating objects
example:
string fName, lName;
creates two objects of the string class

7-9
Introduction to Classes

• Class declaration format:

class className
{
member declaration;
member declaration;
};
Notice the
semicolon is
required

7-10
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
Access
Specifier
• Can be listed in any order in a class class Square

• Can appear multiple times in a class {


private:
• If not specified, the default is private int side;

public:
void setSide(int s) { side = s; }
Access int getSide() { return side; }
Specifier
};

7-11
7.4 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();

7-12
7.5 Defining Member Functions

• Member functions are part of a class


declaration
• Can place entire function definition inside the
class declaration

or

• Can place just the prototype inside the class


declaration and write the function definition
after the class

7-13
Defining Member Functions Inside the Class Declaration

• Member functions defined inside the class declaration are


called inline functions
• Only very short functions, like the ones below, should be
inline functions
(Restricted to one statement for COSC1020 & COSC052)

class Square
{ Both setSide and
private:
getSide are inline
functions
int side;

public:
void setSide(int s) { side = s; }
int getSide() { return side; }
};

7-14
Defining Member Functions After the Class Declaration

class Square Function Prototypes


go inside the class
{
declaration
• Put a function prototype private:
int side;
in the class declaration
public:
void setSide(int s);
int getSide();
• In the function definition, };
precede the function
int main()
name with the class {
return 0;
name and scope }
resolution operator ::
int Square::getSide()
{
Implementation code is return side;
placed somewhere after }
the class declaration
(after function main is a void Square::setSide(int s)
convenient location)
{
side = s;
}
7-15
Types of Member Functions

• Acessor, get, getter function:


uses but does not modify a member variable

• Mutator, set, setter function:


modifies a member variable

class Square
{
private:
int side;
Mutator
function public:
void setSide(int s) { side = s; }
int getSide() { return side; }

Accessor };
function

7-16
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: calculate values to be returned in


accessor functions when possible, to minimize
the potential for stale data

7-17
Tradeoffs of Inline vs. Regular Member Functions

• When a regular function is called, control passes


to the called function
– the compiler 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
– This makes a larger executable program, but
– There is less function call overhead, and possibly
faster execution

7-18
7.6 Constructors
• A constructor is a member function that is
often used to initialize data members of a class
• Is called automatically when an object of the
class is created
• It must be a public member function
• It must be named the same as the class
• It must have no return type
• A class can have more than one constructor
– This is function overloading
– Overloaded functions must have different parameter lists

7-19
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 must have a programmer-
defined default constructor

7-20
Default Constructor Example
class Square class Square
{ {
private: private: Has parameter(s)
but each
int side; int side; parameter is
assigned a
default value
public: public:
//default constructor //default constructor
Square(); Square(int s = 1);

// Other member // Other member


Has no
// functions go parameter(s)
here // functions go here
}; };

Square::Square() Square::Square(int s)
{ {
side = 1; side = s;
} }

7-21
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 square1(8);

7-22
7.7 Destructors

• Is a public member function automatically


called when an object is destroyed
• The destructor name is ~className
~Square()
• It has no return type
• It takes no arguments
• Only 1 destructor is allowed per class
(it cannot be overloaded)

7-23
7. 8 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

7-24
7.9 Passing Objects to Functions

• A class object can be passed as an


argument to a function
• When passed by value, function makes a
local copy of object. Original object in
calling environment is unaffected by
actions in function
• When passed by reference, function can
use ‘set’ functions to modify the object.

7-25
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 program, but allows the
function to modify data in the parameter
• To save space and time, while protecting parameter data that should
not be changed, use a const reference parameter

void showData(const Square &s)

• For the showData function to call Square member functions, those


functions must use const in their prototype and header:
int Square::getSide() const;

7-26
Returning an Object from a Function

A function can return an object


// prototype
Square initSquare();

// function call Square initSquare()


s1 = initSquare();
{
// local variable
Square s;
int inputSize;
The function must cout << "Enter side length: ";
define an object cin >> inputSize;
for internal use s.setSide(inputSize);
and to return return s;
}

7-27

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy