Presentation is loading. Please wait.

Presentation is loading. Please wait.

Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.

Similar presentations


Presentation on theme: "Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction."— Presentation transcript:

1 Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction to classes in C++  Reading: By next Wednesday, Chapter 12 through 12.4.a  Exercises: p. 304 #1, 7, 8, 9, 11, 17

2 Wednesday, 10/2/02, Slide #2 Choosing between value and reference parameters  Value parameter: IN only  For information sent into the function, that is to be used but not changed  No ampersand! Safer. Calling argument can be a variable, a constant, or an expression.  Reference parameter: OUT or IN/OUT  For information sent out of the function,  For information that is sent into the function, then changed, then sent back out of the function  Use ampersand! Calling argument must be a variable.

3 Wednesday, 10/2/02, Slide #3 Example: Choosing value v. reference  A teacher for a class keeps a running weighted average for her students. When a new grade is produced, she uses a function with four parameters to update the average. Write a function UpdateAverage() with four parameters to do this:  average: the current weighted average  averageWt: an integer representing the total weight of all grades processed so far.  grade: the new grade to be processed  gradeWt: the weight of the new grade Example: Current average = 83.2 Total weight so far = 68 Recent exam grade = 95 Exam weight = 10 New average = (83.2*68 + 95 * 10) / (68+10) = 84.7128 New total weight = 68+ 10 = 78

4 Wednesday, 10/2/02, Slide #4 Object-Oriented Programming  An object in high-level languages such as C++ has  A name all the instances of the object have in common  Some attributes - its parts or components  Some functions (or methods) - things you want to do with the object  Examples: int, double, a complex number, a Skidmore student record

5 Wednesday, 10/2/02, Slide #5 OOP and the C++ class structure  OOP is programming done according to the following three principles:  ENCAPSULATION: Use objects (so in C++, classes) to combine data and operations  INHERITANCE: Permit new objects to inherit properties from previously defined objects  POLYMORPHISM: Allow the meanings of operations to be determined by the object and arguments executing that operation

6 Wednesday, 10/2/02, Slide #6 Abstract Data Types, Objects and Classes  ABSTRACT DATA TYPE: Well-defined collection of data and a set of operations on that data  Examples: integer, double, complex number, Skidmore student record  OBJECT: The implementation of an ADT into a high- level language, encapsulating the data and operations.  Examples: C++ objects of type int, double, etc.; Programmer-defined classes such as Complex  CLASS: A C++ construct used to implement user- defined objects to represent ADT’s.  Examples: Complex, Student_Record

7 Wednesday, 10/2/02, Slide #7 Example: Complex Numbers From ADT to class  ADT: Instances: a complex number a + b i Operations: Input, Output, arithmetic  Implementation as C++ class:  Class name: Complex  Data members (attributes): double real; double imag;  Functions (operations, methods): InputC(), OutputC(), AddC(), etc.  Class file named something like Complex.h:  the “header” / “definition” / “interface” file  Contains declarations of data members: double real, imag;  Contains prototypes of member functions  Class file named something like Complex.cpp:  the “implementation” file  Contains implementations of member functions

8 Wednesday, 10/2/02, Slide #8 The header (interface, definition) file  The header file for a class contains:  Prototypes for all the member functions  Declarations of the data members (or attributes) that comprise the data in the object  These are grouped into two sections:  public: class members that clients are permitted to use  Usually contains all the functions provided for clients  private: class members that clients are not permitted to use  Usually contains all the data members  May also contain some member functions (typically auxiliary functions used to implement the public functions)  Look at Complex1.h

9 Wednesday, 10/2/02, Slide #9 The implementation file  Contains implementations of the member functions  Function name must be prefixed with the class name and scope operator: Complex::  This indicates that the function will be invoked by a class object  The invoker is a sort of ‘un-named parameter’  In function definition:  Refer to members (data or function) of the invoking object without dot notation  If there are any parameters or local objects from this class, they do need the dot notation!  See Complex1.cpp

10 Wednesday, 10/2/02, Slide #10 Client use of classes  Client Use: To use objects of type Complex in a C++ program, e.g. ComplexTest.cpp  Include the header file: #include “Complex.h”  Put implementation file (Complex.cpp) in project  To declare a Complex object num, the client types a line similar to one to declare an int object i:  Complex num; //compare with int i;  To apply member functions to L, the client uses the member access operator (symbol ‘.’):  num.InputC();  See ComplexTest1.cpp

11 Wednesday, 10/2/02, Slide #11 Constructors  Constructors are member functions that are called when a class object is declared:  Complex num1;  Complex num2(3,5); //complex number 3 + 5 i  Job of constructor is to set up the private data members of the object:  If no initial values (num1), constructor body can be empty! Called “Default constructor”  If initial values (num2), they are assigned to data members in body of the constructor

12 Wednesday, 10/2/02, Slide #12 Constructor Syntax  Name of constructor must be the class name  Constructors have no return type  Complex has two constructors:  Complex ( ) { };  Default constructor; empty body included in prototype in header file  Complex (float initreal, float initimag) ;  Constructor that initializes data members with parameter values; body appears in implementation  Client use: ConstructorName ObjectName(Constructor Parameters)  Notice that parameter list follows object name, not function name!  Number and type of parameters tells the compiler which constructor function to use -- function overloading


Download ppt "Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction."

Similar presentations


Ads by Google