Learning Objectives Classes Constructors Principles of OOP

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Defining Classes.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Defining Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
Chapter 10 Defining Classes. Slide Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types.
1 Chapter 10 Defining Classes. 2 Overview Introduction Classes (10.2) Abstract Data Types and Structures (10.1)(10.3) Abstract Data Types and Structures.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Classes Definition A class is a data type whose variables are objects Object – a variable that has member functions as well the ability to hold.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Chapter 6 Defining Classes. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Introduction Structures (6.1) Classes (6.2)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
Friend Functions.  An ordinary function that is given special access to the private members of a class  NOT a member function of the class  Prototype.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
General Updates ● The Wiki is now back up to date. I have also added a working link to the lecture slides online. ● Whenever you notice something missing.
COMP 53 – Week Two Operator Overloading.
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
Structures and Classes
Classes (Part 1) Lecture 3
Classes C++ representation of an object
What header file contains C++ file I/O instructions?
Class and Objects UNIT II.
Review What is an object? What is a class?
Chapter 10 Defining Classes. Chapter 10 Defining Classes.
Andy Wang Object Oriented Programming in C++ COP 3330
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
COMP 51 Week Twelve Classes.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
More about OOP and ADTs Classes
Lecture 4-7 Classes and Objects
C++ Classes & Object Oriented Programming
Corresponds with Chapter 7
Classes and Data Abstraction
More about OOP and ADTs Classes
Constructors and Other Tools
Classes and Objects.
Friends, Overloaded Operators, and Arrays in Classes
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
Classes C++ representation of an object
Final and Abstract Classes
Classes and Objects Systems Programming.
Presentation transcript:

Learning Objectives Classes Constructors Principles of OOP Class type member data

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

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

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()

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

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

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

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

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

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

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(); };

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

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

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

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!

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

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!

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

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.

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

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

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