L7-Classes Intro PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Intro to Classes (OOD)

CptS 122 – Fall 2019


Washington State University
• Defining classes with member functions
• Setters (mutators) and getters
(accessors)
Concepts we
• The Rule of Three, Law of The Big Three,
will Cover or The Big Three
• Constructors
• Default and copy
• Destructors

Nadra Guizani
Class (User defined Type)
• A class consists of data members (attributes) and member functions
(operations)

• An object is an instantiation of a class

• A class controls access to its members

• Typically you cannot call a member function unless an object of the class
has been instantiated
• One exception to this rule is when you declare a member function with the
keyword static

Nadra Guizani
Class (User defined Type)
• Classes allow the developer to separate interfaces from
implementation, which is a principle of good software engineering

• Generally function prototypes for member functions are placed in the


class.h file and the implementation for these in the class.cpp file

• The function prototypes describe the classes public interfaces without


exposing the internal implementation of the member functions

Nadra Guizani
Class Declaration

Nadra Guizani
Access Modifiers
• Private – members that cannot be accessed by the instance of the
class

• Public – access to a class’s private member variables, you create


public member functions that work with the private member
variables
• Protected – will talk about when we enter the topic on inheritance

Nadra Guizani
Setters and Getters
• These are public interfaces/functions to provide access to
private data members

• Setters allow an object to set or modify the data members

• Getters allows for an object to obtain/get a copy of the data members

• There generally should be 1 setter function per data member, and 1


getter function data member (of course this depends on whether or
not a data member should be accessed by an object)

Nadra Guizani
Rectangle Class Example
Rectangle.h
Rectangle.cpp
main.cpp

Nadra Guizani
Rectangle Class Object
• Data Members
• Length
• Width
• Functionalities
• Area
• Perimeter
• Square?

Nadra Guizani
Continuation of the Rectangle class
•Copy constructors

•Overloading operators

Nadra Guizani
Constructors

• Member Function taking on the same name as the class name

• it cannot return a value, and it is called implicitly when an object is instantiated

• Generally constructors are declared public

• When is an object instantiated?


• When a variable of the type of class is declared

• When the new operator is explicitly invoked (new à malloc())

Nadra Guizani
The Rule of Three
• Also known as the Law of The Big Three or The Big Three

• The rule states that if one or more of the following are defined, then
all three should be explicitly defined
– Destructor
– Copy constructor
– Copy assignment operator

Nadra Guizani
Copy Constructor
• A copy constructor always accepts a parameter, which is a reference
to an object of the same class type
Rectangle (Rectangle &copyObject);

• Copy constructors make a copy of an object of the same type

• A copy constructor is implicitly invoked when an object is passed-by-


value!

• A deep copy is made if new memory is allocated for each of the data
members
NOTE: The = operator may be used to assign one object's data to another
object, or to initialize one object with another object's data
Destructor
• Each class declared provides a destructor

• destructor is a special member function because it MUST also be


named the same as the class (with a tilde (~) in front) it cannot return
a value, and it is called implicitly when an object is destroyed
~ComplexNumber ();
• If a class does not explicitly provide a destructor, then the compiler
provides an “empty” destructor
• When does an object get destroyed?
– When the object leaves scope
– When the delete operator is explicitly invoked (free())
Nadra Guizani
Friend functions

• A friend is a function or class that is not a member of a class

• But has access to the private members of the class.

• Friend function is treated as if it were a member of the class

• Classes keep a “list” of their friends

• Only the external functions or classes whose names appear in the list
are granted access
Nadra Guizani
Overloading Operators
• C++ allows you to redefine how standard operators work when used
with class objects.
• Arithmetic
• Relational
• Stream

• You have already experienced the behavior of an overloaded


operator.

• The / operator performs two types of division:


• floating point
• integer
Nadra Guizani
Overloading the + Operator

Nadra Guizani
Overloading the << and >> Operators

Nadra Guizani
Checkpoint Question
• True or False: You must declare all private members of a class before
the public members.

• An object’s private member variables are accessed from outside the


object by what?

• Assume that RetailItem is the name of a class, and the class has a
double private member and a void member function named setPrice,
which accepts a double argument. Write the code for this class.

Nadra Guizani
Class Example: Complex Numbers
Declaration
Data Members
Member Function

Nadra Guizani
Class Example: Complex Numbers
Constructors

Nadra Guizani
References
• Chapter 13: C++ Starting from control structures through objects 8th
edition.

• P.J. Deitel & H.M. Deitel, C++: How to Program (9th ed.), Prentice Hall,
2014.
• J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C
(7th Ed.), Addison- Wesley, 2013

Nadra Guizani
Overloading Operator

Nadra Guizani
Overloading Operator (=)

Nadra Guizani
Overloading Operator (=)

Nadra Guizani
Overloading Operator (<<)

Nadra Guizani

You might also like