A CLASS CONSISTS OF VARIABLES,

Slides:



Advertisements
Similar presentations
Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
Inheritance and Composition (aka containment) Textbook Chapter –
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
IT PUTS THE ++ IN C++ Object Oriented Programming.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
1 Chapter 9 Scope, Lifetime, and More on Functions.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
CSC 142 Computer Science II Zhen Jiang West Chester University
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
Chapter 11 Separate Compilation and Namespaces. Learning Objectives Separate Compilation –Encapsulation reviewed –Header and implementation files Namespaces.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Exercise 2 Introduction to C# CIS Create a class called Employee that contains the following private instance variables: Social Securitystring.
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.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 C++ Classes and Data Structures Course link…..
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Chapter 1.2 Introduction to C++ Programming
Object-Oriented Concepts
Chapter 22 - C++ Templates
OOP: Encapsulation &Abstraction
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ Systems Programming.
Andy Wang Object Oriented Programming in C++ COP 3330
Bill Tucker Austin Community College COSC 1315
Chapter Structured Types, Data Abstraction and Classes
Introduction to Structured Data Types and Classes
Enumeration used to make a program more readable
Chapter 9 Scope, Lifetime, and More on Functions
3.3 Abstract Classes, Assignment, and Casting in a Hierarchy
Inheritance I Class Reuse with Inheritance
CMSC 202 Inheritance.
Starting Out with C++: From Control Structures through Objects
Class Reuse with Inheritance
C++ File Structure and Intro to the STL
Classes.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 19 - Inheritance Outline 19.1 Introduction
Recitation Course 0610 Speaker: Liu Yu-Jiun.
C++ File Structure and Intro to the STL
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
Class rational part2.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 7 Inheritance.
Presentation transcript:

A CLASS CONSISTS OF VARIABLES, CALLED FIELDS, TOGETHER WITH FUNCTIONS, CALLED METHODS, THAT ACT ON THOSE FIELDS.

USER’S VIEW OF A CLASS: METHOD INTERFACES (= PRECONDITION + POSTCONDITION + METHOD HEADING) DEVELOPER’S VIEW OF A CLASS: FIELDS AND METHOD DEFINITIONS

BEST-PAID EMPLOYEE IN A COMPANY? CREATE A CLASS CALLED Employee. THE INFORMATION AVAILABLE ON EACH EMPLOYEE CONSISTS OF THE EMPLOYEE’S NAME AND GROSS PAY.

THE Employee CLASS USER’S VIEW

THE Employee CLASS: USER’S VIEW // Postcondition: this Employee's name has been set to // “” and gross pay to 0.00. Employee( );

THE Employee CLASS: USER’S VIEW // Postcondition: This Employee's name has been set to // “” and gross pay to 0.00. Employee( ); NOTE: IN THE ABOVE POSTCONDITION, AND IN THE POSTCONDITIONS THAT FOLLOW, “this Employee” REFERS TO THE CALLING OBJECT: THE OBJECT THAT CALLED THE METHOD.

THE Employee CLASS: USER’S VIEW // Postcondition: The name and gross pay of this // Employee been read in. void readInto( );

THE Employee CLASS: USER’S VIEW // Postcondition: true has been returned if this // Employee is the sentinel. Otherwise, // false has been returned. bool isSentinel( ) const; NOTE: THE RESERVED WORD const AT THE END OF THE METHOD HEADING MEANS THAT THE DEFINITION OF THE METHOD IS NOT ALLOWED TO MODIFY THE CALLING OBJECT.

THE Employee CLASS: USER’S VIEW // Postcondition: true has been returned if this // Employee's gross pay is greater than // that of otherEmployee. Otherwise, false // has been returned. bool makesMoreThan (const Employee& otherEmployee) const;

THE Employee CLASS: USER’S VIEW // Postcondition: true has been returned if this // Employee's gross pay is greater than // that of otherEmployee. Otherwise, false // has been returned. bool makesMoreThan (const Employee& otherEmployee) const; DEFINITION NOT ADDRESS OF ARGUMENT ALLOWED TO MODIFY SENT, NOT A COPY ARGUMENT OF ARGUMENT (PROVIDES SECURITY) (SAVES TIME AND SPACE)

THE Employee CLASS: USER’S VIEW // Postcondition: this Employee contains a copy of // otherEmployee. void getCopyOf (const Employee& otherEmployee); // Postcondition: this Employee's name and gross pay // have been written out. void printOut( ) const;

Employee OBJECTS Employee employee1, employee2; employee1.readInto( ); if (employee1.makesMoreThan (employee2)) employee1.printOut( ); else employee2.printOut( );

THE Employee CLASS:. DEVELOPER’S VIEW employee1 THE Employee CLASS: DEVELOPER’S VIEW employee1.h (THE HEADER FILE) #ifndef EMPLOYEE #define EMPLOYEE #include <string> // declares string class using namespace std; class Employee {

THE Employee CLASS: DEVELOPER’S VIEW employee1.h (continued) public: // Postcondition: this employee's name has // been set to "" and gross pay // to 0.00. Employee( ); // method interfaces for other methods

THE Employee CLASS:. DEVELOPER’S VIEW employee1. h (continued) THE Employee CLASS: DEVELOPER’S VIEW employee1.h (continued) private: string name; double grossPay; const static double GROSS_PAY_SENTINEL; const static string EMPTY_STRING; const static string NAME_SENTINEL; }; // Employee #endif

THE Employee CLASS: DEVELOPER’S VIEW employee1.cpp (THE SOURCE FILE ) #include <iostream> #include <iomanip> // declares output formatting objects #include "employee1.h" // declares Employee class

THE Employee CLASS:. DEVELOPER’S VIEW employee1. cpp (continued ) THE Employee CLASS: DEVELOPER’S VIEW employee1.cpp (continued ) Employee::Employee( ) { name = EMPTY_STRING; grossPay = 0.00; } // default constructor

scope-resolution employee1 scope-resolution employee1.cpp (continued ) operator (the readInto method in void Employee::readInto( ) the Employee class) { const string NAME_AND_PAY_PROMPT = "Please enter a name and gross pay, to quit, enter "; cout << NAME_AND_PAY_PROMPT << NAME_SENTINEL << " " << GROSS_PAY_SENTINEL; cin >> name >> grossPay; } // readInto

employee1. cpp (continued ). // definitions of other employee methods employee1.cpp (continued ) // definitions of other employee methods ... const string Employee::EMPTY_STRING = ""; const string Employee::NAME_SENTINEL = "*"; const double Employee::GROSS_PAY_SENTINEL = -1.0;

FIND THE BEST PAID EMPLOYEE APPLICATION: FIND THE BEST PAID EMPLOYEE IN A COMPANY.

The Company Class: User’s View // Postcondition: this Company has been initialized. Company( ); // Postcondition: this Company’s best‑paid employee // has been determined. void findBestPaid( ); // has been printed out. void printBestPaid( ) const;

THE Company CLASS: DEVELOPER’S VIEW SEE LAB 1

EXERCISE: TO GET THINGS STARTED, THE main FUNCTION MUST DEFINE A Company OBJECT. PROVIDE THAT DEFINITION. WHAT FILE (employee1.h, employee1.cpp, company1.h, company1.cpp) MUST BE INCLUDED IN THE CLASS THAT HAS THE main FUNCTION?  

SUPERCLASS SUBCLASS

THE OPEN-CLOSED PRINCIPLE EVERY CLASS SHOULD BE OPEN: EXTENDIBLE THROUGH INHERITANCE CLOSED: STABLE FOR EXISTING APPLICATIONS

THE NEW CLASS WILL BE HourlyEmployee A SUBCLASS OF Employee. WHICH Employee METHODS WILL BE OVERRIDDEN?

THE makesMoreThan, getCopyOf and printOut METHODS ARE UNTOUCHED, AND MAY BE CALLED AS IS BY ANY HourlyEmployee OBJECT AFTER ALL, AN HourlyEmployee IS AN Employee!

WHAT FIELDS? FOR THE NEW INFORMATION: hoursWorked, payRate

WHAT ABOUT name AND grossPay? ACCORDING TO THE DEFINITION OF INHERITANCE, THE HourlyEmployee CLASS AUTOMATICALLY “INHERITS” THESE TWO FIELDS. BUT HOW?

TO ALLOW SUBCLASS OBJECTS TO ACCESS SUPERCLASS FIELDS, THOSE FIELDS SHOULD BE GIVEN THE protected LEVEL OF PROTECTION. private IS TOO RESTRICTIVE (SUPERCLASS ONLY) public IS TOO LAX (ANY CLASS’S CODE CAN ACCESS THE FIELD)

SO, IN THE Employee CLASS, WE CHANGE THE PROTECTION LEVEL FOR THOSE TWO FIELDS: protected: string name, double grossPay;

THEN THOSE TWO FIELDS CAN BE ACCESSED IN ANY Employee CLASS METHOD, AND IN ANY METHOD IN ANY SUBCLASS OF Employee. HERE IS THE DECLARATION OF THE HourlyEmployee CLASS (THE POST- CONDITIONS WERE GIVEN ABOVE):

THE METHOD DEFINITIONS ARE JUST WHAT YOU WOULD EXPECT. FOR EXAMPLE, HERE IS THE DEFINITION OF isSentinel:

WE CREATED THIS HourlyEmployee CLASS FOR THE APPLICATION OF FINDING THE BEST-PAID HOURLY EMPLOYEE IN A COMPANY. CAN THE ORIGINAL Company CLASS USE THE HourlyEmployee CLASS FOR THIS NEW APPLICATION?

NO, BECAUSE THE Company CLASS MAKES NO MENTION OF AN HourlyEmployee OBJECT. SO WE WILL DEFINE Company2, A SUBCLASS OF Company. HERE IS THE DECLARATION OF THE Company2 CLASS: