Presentation is loading. Please wait.

Presentation is loading. Please wait.

Learning Objectives Classes Constructors Principles of OOP

Similar presentations


Presentation on theme: "Learning Objectives Classes Constructors Principles of OOP"— Presentation transcript:

1 Learning Objectives Classes Constructors Principles of OOP
Class type member data

2 Topic 1: Classes Similar to structures Object-oriented programming
Adds member FUNCTIONS Not just member data Object-oriented programming Class: Abstract data type that contains data and operations Object: Instances of a class

3 Class Definitions Similar to structures
Example: class DayOfYear { public: int month; int day; public : void output(); };

4 Declaring Objects Declared same as all variables
Example: DayOfYear today, birthday; Declares two objects of class type DayOfYear Objects include: Data (member data) month, day Operations (member functions) output()

5 Class Member Access Members accessed same as structures
Example: today.month today.day And to access member function: today.output();  Invokes member function

6 Class Member Functions
Must define or "implement" class member functions Like other function definitions Must specify class: return_type class_name::function_name(parameters) Example: void DayOfYear::output() {…} :: is scope resolution operator Instructs compiler "what class" member is from Item before :: called type qualifier

7 Dot and Scope Resolution Operator
Dot (.) operator: Specifies member of particular object Scope resolution (::) operator: Specifies what class the function definition comes from

8 Class Member Functions Definition
Function used for all objects of the class Will refer to "that object’s" data when invoked Example: today.output(); Displays "today" object’s data

9 Topic 2: Constructors of Classes
Initialization of objects Initialize some or all member variables Other actions possible as well, but not typicall A special kind of member function Automatically called when object declared

10 Constructor Definitions
Constructors defined like any member function Except: Must have same name as class Cannot return a value; not even void!

11 Constructor Definition Example
Class definition with constructor: class DayOfYear { public: int month; int day; public: DayOfYear(int m, int d); //Constructor initializes month & day void output(); };

12 Constructor Code Constructor definition is like all other member functions: DayOfYear::DayOfYear(int m, int d) { month = m; day = d; }

13 Constructor Notes Notice name of constructor: DayOfYear
Same name as class itself! Constructor declaration has no return-type Not even void! Constructor in public section

14 Calling Constructors Declare objects: DayOfYear date1(7, 4);
Objects are created here Constructor is called Values in parenthes passed as arguments to constructor

15 Constructor Equivalency
Consider: DayOfYear date1, date2 date1.DayOfYear(7, 4); // ILLEGAL! date2.DayOfYear(5, 5); // ILLEGAL! Seemingly OK… CANNOT call constructors like other member functions!

16 Overloaded Constructors
Can overload constructors just like other functions Recall: a signature consists of: Name of function Parameter list Provide constructors for all possible argument-lists

17 Constructor with No Arguments
Can be confusing Standard functions with no arguments: Called with syntax: callMyFunction(); Including empty parentheses Object declarations with no "initializers": DayOfYear date1; // This way! DayOfYear date(); // Not this way!

18 Topic 3: Principles of OOP
Information Hiding Details of how operations work not known to "user" of class Data Abstraction Details of how data is handled within class not known to user Encapsulation Bring together data and operations, but keep "details" hidden

19 Public and Private Members
In C++, Principles of OOP are implemented through the qualifier public and private keywords Private data in class allows manipulation only via member functions Public items (usually member functions) can be accessed by other functions.

20 Public and Private Example
This class definition satisfies principles of OOP class DayOfYear { private: int month; int day; public: void input(); void output(); }; Data now private Objects have no direct access to private data

21 Public and Private Example (continued)
DayOfYear d; …….. d.month =1; // No longer allowed d.input(); // This is allowed How data members are handled is hidden

22 Topic 4: Class Type Member Variables
Class member variables can be any type Including objects of other classes!


Download ppt "Learning Objectives Classes Constructors Principles of OOP"

Similar presentations


Ads by Google