Download presentation
Presentation is loading. Please wait.
1
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7
3
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
4
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
5
3.7 Extending C++ through Classes –Discuss two classes String class part of compiler Money class is a user defined class –#include –#include “money.h”
6
String Class Declaring string objects Reading & Displaying strings Assignment & Concatenation Operator Overloading Dot Notation Member Functions Object Assignments
7
Declaring string Objects A number of ways to declare string firstName, lastName; string wholeName; string greeting = “Hello “;
8
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;
9
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
10
Assignment Stores the first and last name –wholeName = firstName + “ “ + lastName; Concatenation –+ joins the two objects together “ “ for string values not ‘ ‘
11
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
12
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
13
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
14
Money Class Process money objects Two attributes –dollars –cents Why do this ?? Why not float ??
15
Money Class Allocate a money object –money creditLimit = 5000.00; Assignments –taxAmount = salePrice * taxPercent/100.0; –finalCost = salePrice + taxAmount;
16
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
17
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
18
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
19
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
20
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
21
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
22
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
23
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
24
Syntax Form: class className { public: List all public data and functions private: List all private data and functions };
25
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
26
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
27
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
28
Compiler Directives Prevent multiple definitions #ifndefCOUNTER_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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.