COSC1020-Lecture18
COSC1020-Lecture18
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
7-3
Abstraction and Data Types
7-4
7.2 Object-Oriented Programming
7-5
Object-Oriented Programming Terminology
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);
7-8
7.3 Introduction to Classes
7-9
Introduction to Classes
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:
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
or
7-13
Defining Member Functions Inside the Class Declaration
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
{
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
7-17
Tradeoffs of Inline vs. Regular Member Functions
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
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);
Square::Square() Square::Square(int s)
{ {
side = 1; side = s;
} }
7-21
Invoking a Constructor
7-22
7.7 Destructors
7-23
7. 8 Private Member Functions
7-24
7.9 Passing Objects to Functions
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
7-26
Returning an Object from a Function
7-27