CPSC 231 C++ Review1 Learning Objectives §Review of the object oriented design goals. §Review of C++ classes l data members l member functions (methods)

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

Operator overloading redefine the operations of operators
C++ Classes & Data Abstraction
Chapter 20- Virtual Functions and Polymorphism Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng.
Esempi Ereditarietà1 // Definition of class Point #ifndef POINT_H #define POINT_H #include using std::ostream; class Point { friend ostream &operator
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6: Classes and Data Abstraction Outline 6.1 Introduction 6.2 Structure Definitions 6.3 Accessing.
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction 16.2Implementing a Time Abstract Data.
Introduction to Classes and Data Abstraction
1 Review (Week2) - Control Structures C++ has only seven control structures: –Sequence structure Programs executed sequentially by default –Selection structures.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
 2003 Prentice Hall, Inc. All rights reserved Introduction Object-oriented programming (OOP) –Encapsulates data (attributes) and functions (behavior)
Classi - Esempi1 // SalesPerson class definition // Member functions defined in salesp.cpp #ifndef SALESP_H #define SALESP_H class SalesPerson { public:
Software Engineering 1 (Chap. 1) Object-Centered Design.
1 Object-Oriented Programming Using C++ CLASS 27.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
1 Chapter 4 CLASSES AND OBJECTS. 2 Outlines Procedural vs. Object ‑ Oriented Programming C++ Structures versus C Structures Classes –Accessing Class Members.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction 15.2C A Simple Program: Adding Two Integers.
1 Chapter-01 Introduction to Software Engineering.
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes.
11 Introduction to Object Oriented Programming (Continued) Cats.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance Part 2 Outline 19.8Direct Base Classes and Indirect Base Classes 19.9Using Constructors.
1 Classes and Data Abstraction Part I Introduction Object-oriented programming (OOP)  Encapsulates data (attributes) and functions (behavior)
1 C++ Classes: Access (II) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 6: Classes and Data Abstraction Outline 6.1Introduction 6.2Structure Definitions 6.3Accessing.
Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given.
Class 3 (L33) u Client of a Class u Purpose of Public Members u Private Class Members u Controlling Access u Access Functions u Predicate Functions u Utility.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3: Classes May 24,
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 4: Classes Jan 27,
1 Lecture 6 Classes and Data Abstraction: Part II.
Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Computer Engineering 2 nd Semester Dr. Rabie A. Ramadan 3.
11 Introduction to Object Oriented Programming (Continued) Cats.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Polymorphism Outline 20.5Polymorphism 20.6Case Study: A Payroll System Using Polymorphism.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
1 Class 19 Chapter 13 – Creating a class definition.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 CSE 2341 Object Oriented Programming with C++ Note Set #12.
Seventh step for Learning C++ Programming CLASS Declaration Public & Private Constructor & Destructor This pointer Inheritance.
1 Example Original Array Array After 2 nd Pass Array After 1 st Pass Array After 3 rd Pass.
Chapter 16: Classes and Data Abstraction
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Abstract Data Types Programmer-created data types that specify
Classes.
Chapter 5 Classes.
solve the following problem...
CPSC 231 D.H. C++ File Processing
Lecture 5: Classes September 14, 2004
Chapter 6: Classes and Data Abstraction
Chapter 6: Classes and Data Abstraction
Object-oriented programming (OOP)
Chapter 6: Classes and Data Abstraction
Chapter 15 - C++ As A "Better C"
CISC181 Introduction to Computer Science Dr
NAME 436.
Chapter 6: Classes and Data Abstraction
Chapter 6: Classes and Data Abstraction
Chapter 11 Classes.
Presentation transcript:

CPSC 231 C++ Review1 Learning Objectives §Review of the object oriented design goals. §Review of C++ classes l data members l member functions (methods) l constructors l destructors

CPSC 231 C++ Review2 C++ Classes §Classes enable programmers to model objects that have attributes (data members) and behaviors (member functions or methods) §Once a class has been defined, the class name can be used to declare objects of that class.

CPSC 231 C++ Review3 A Simple C++ Class #include // a Box class class Box { private: int height, width, depth; // private data members public: Box(int, int, int); // constructor function ~Box(); // destructor function int volume(); // member function (computev olume) };

CPSC 231 C++ Review4 Class Box the constructor function Box::Box(int ht=0, int wd=0, int dp=0) { height = ht; width = wd; depth = dp; } // the destructor function Box::~Box() { // does nothing } //

CPSC 231 C++ Review5 Class Box cont. // member function to compute the Box's volume int Box::volume() { return height * width * depth; } // an application to use the box int main() { Box b, thisbox(7,8,9); // declare a Box cout <<b.volume()<<' '<<thisbox.volume(); // compute & display return 0; }

CPSC 231 C++ Review6 Example of a SalesPerson Class // salesp.h p 384 of D&D // SalesPerson class definition // Member functions defined in salesp.cpp #ifndef SALESP_H #define SALESP_H class SalesPerson { public: SalesPerson(); // constructor void getSalesFromUser(); // get sales figures from keyboard void setSales( int, double ); // user supplies data void printAnnualSales();

CPSC 231 C++ Review7 SalesPerson class definition cont. private: double totalAnnualSales(); // utility function double sales[ 12 ]; // 12 monthly sales figures }; #endif

CPSC 231 C++ Review8 Member Functions for Class SalesPerson // salesp.cpp // Member functions for class SalesPerson #include #include "salesp.h" // Constructor function initializes array SalesPerson::SalesPerson() { for ( int i = 0; i < 12; i++ ) sales[ i ] = 0.0; }

CPSC 231 C++ Review9 “get” function getSalesFromUser // Function to get 12 sales figures from the user // at the keyboard void SalesPerson::getSalesFromUser() { double salesFigure; for ( int i = 0; i < 12; i++ ) { cout << "Enter sales amount for month " << i + 1 << ": "; cin >> salesFigure; setSales( i, salesFigure ); }

CPSC 231 C++ Review10 “set” function setSales // Function to set one of the 12 monthly sales figures. // Note that the month value must be from 0 to 11. void SalesPerson::setSales( int month, double amount ) { if ( month >= 0 && month 0 ) sales[ month ] = amount; else cout << "Invalid month or sales figure" << endl; }

CPSC 231 C++ Review11 “print” function printAnnualSales // Print the total annual sales void SalesPerson::printAnnualSales() { cout << setprecision( 2 ) << setiosflags( ios::fixed | ios::showpoint ) << "\nThe total annual sales are: $" << totalAnnualSales() << endl; }

CPSC 231 C++ Review12 “utility” function totalAnnualSales // Private utility function to total annual sales double SalesPerson::totalAnnualSales() { double total = 0.0; for ( int i = 0; i < 12; i++ ) total += sales[ i ]; return total; }

CPSC 231 C++ Review13 Sample driver // Compile with salesp.cpp #include #include "salesp.h" int main() { SalesPerson s; // create SalesPerson object s s.getSalesFromUser(); // note simple sequential code s.printAnnualSales(); // no control structures in main return 0; }