Professional Documents
Culture Documents
L7-Classes Intro PDF
L7-Classes Intro PDF
L7-Classes Intro PDF
Nadra Guizani
Class (User defined Type)
• A class consists of data members (attributes) and member functions
(operations)
• 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
Nadra Guizani
Class Declaration
Nadra Guizani
Access Modifiers
• Private – members that cannot be accessed by the instance of the
class
Nadra Guizani
Setters and Getters
• These are public interfaces/functions to provide access to
private data members
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
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 ©Object);
• 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
• 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
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.
• 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