CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

28
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    222
  • download

    1

Transcript of CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Page 1: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

CS 117 Spring 2002

Classes

Hanly: Chapter 6

Freidman-Koffman: Chapter 10, intro in Chapter 3.7

Page 2: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
Page 3: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Classes

• Classes are what makes C++ object oriented– you can create your own types– encapsulate - hide details from the user– simplifies development by separating a

program into nearly independent parts

Page 4: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Examples

• Classes you've already seen– string– istream (cin) and ostream (cout)

• classes you could have used– point class that holds an x and a y value (or an r

and a theta value)– amino class to hold amino acid name and

number of atoms of of each element

Page 5: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

3.7 Extending C++ through Classes

– Discuss two classes• String class part of compiler• Money class is a user defined class

– #include <string>

– #include “money.h”

Page 6: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

String Class

• Declaring string objects

• Reading & Displaying strings

• Assignment & Concatenation

• Operator Overloading

• Dot Notation

• Member Functions

• Object Assignments

Page 7: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Declaring string Objects

• A number of ways to declare

string firstName, lastName;

string wholeName;

string greeting = “Hello “;

Page 8: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Reading & Displaying string Objects

• Use extraction operator >> & the stream cin for input– cin >> firstName;

• Use insertion operator << & the stream cout for output– cout << greeting << wholeName <<

endl;

Page 9: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Reading & Displaying string Objects

getline (cin, lastName, ‘\n’);

reads all characters typed in from the keyboard up to the new line into the string object

Page 10: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Assignment

• Stores the first and last name– wholeName = firstName + “ “ +

lastName;

• Concatenation– + joins the two objects together

• “ “ for string values not ‘ ‘

Page 11: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Operator Overloading

• + normally means addition but with strings it means concatenation

• The + can take on many meanings– Operator Overloading– C++ can have multi meanings to 1 operator– >> & << are overloaded operators– * / - == = all can be overloaded

Page 12: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Dot Notation

• Dot notation used to call an objects member functions

wholeName.length();– Applies member function length to string object

wholeName– Function returns the objects length

Page 13: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

string functions

int length() returns number of characters

char at( int i)

[i]

returns char at index i

int find( string target) returns index of targget in the string

char[] c_str() returns equivalent C-style string

Page 14: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Money Class

• Process money objects

• Two attributes– dollars– cents

• Why do this ?? Why not float ??

Page 15: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Money Class

• Allocate a money object– money creditLimit = 5000.00;

• Assignments– taxAmount = salePrice * taxPercent/100.0;– finalCost = salePrice + taxAmount;

Page 16: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Class Definition

• int, float, char are built into C+• Declare and use

– int x = 5;

• Create user defined data types+– Extensive use throughout remainder of course

• Counter class will introduce the concept– counter.h counter class definitions– Define the class in the .h file

Page 17: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

User Defined Types

• Define what data we want in the class– data elements of the class are private

• Create the functionality to operate on the data– class member functions access data

• We have created a new data type not known to the compiler

Page 18: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Class Implementation

• Hidden from users (details)• Scope resolution operator

– :: prefix for each member function– Informs the compiler that the function is a member of the class

• Class member functions can access all class data members– Avoid using member functions inside member functions

Page 19: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Constructors

• Two special member functions– Same name as class name

• Constructor executes each time an object of type counter is declared– counter mike;

• Initializes object• Types

– Default– Class

Page 20: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Constructors

• Default Constructor – Used when no arguments passed as part of the

declaration

• Class Constructor– Used when arguments are passed as part of the

declaration

• Constructors do NOT specify a return type

Page 21: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Member Functions

• Member functions that modify data are– setValue– increment– decrement

• Member functions that retrieve data are– getvalue– getmaxvalue

• Const; at the end of the function means no data changes by the function

Page 22: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Summary of Rules for Use of Classes and Objects

• Class Instance– counter c1;– Creates an instance of the counter class (object)– Default constructor invoked– Allocates space in memory for object

• Similar to other data type declarations– int value;– Space in memory allocated for a data type int

Page 23: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Private vs Public

• Public member functions allow users to operate on the counter object c1

• May have private member functions– If member functions need to call another function not

part of the class it should be private

• Use care when defining public access• Users of a class are clients• Class sometimes referred to as the server

Page 24: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Syntax

Form: class className

{

public:

List all public data and functions

private:

List all private data and functions

};

Page 25: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Function Overloading & Polymorphism

• Functions with the same name is called function overloading

• Polymorphism is what allows functions with the same name to do different things based on its arguments

Page 26: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Classes as Operands and Arguments

• Assignment operator can be used• Others must be re-defined (Chap 11)

– Operator Overloading

• Use C& ob1 as formal reference argument• Use const C& ob1 to specify object can’t be

modified by a function– Efficiency issues

Page 27: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Common Programming Errors

• Function argument & return value errors• Not defining a function as a member of a

class• Prefixing a class function call

– obj.function();

• Referencing a private attribute of a class• Missing header files• Missing ; on class definition

Page 28: CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.

Compiler Directives

• Prevent multiple definitions#ifndef COUNTER_H#define COUNTER_H#endif

• 1st the #ifndef tests to see if COUNTER_H has been defined– If not the 2nd line #define defines the content– If defined skips to the #endif