Download presentation
Presentation is loading. Please wait.
Published byReynold Porter Modified over 9 years ago
2
Classes Structured Programming 256 Chapter 8
3
Classes - Part I OOP & Class Object Terminology File Management
4
Object Oriented Programming OOP industry standard C++ is language of choice
5
Object Oriented Programming With OOP and C++ well organized easy to read to design to modify reuse of code facilitated
6
Object Oriented Programming data and their procedures as a single object Key features Encapsulation Data hiding Inheritance Polymorphism * *
7
Object Oriented Programming What is radius? Radius is 5. Circle Object Radius = 5 User Get radius?
8
Abstract Data Type ADT = a programmer defined data type whose properties (domain [values] and operations) are specified independently of any particular implementation. * It has a how and a what.
9
Classes A class is a programmer-defined data type. It consists of data structure and functions which operate on that data.
10
Example of a Class Declaration class Student { public: void PrintGrade(); void addGrade(double grade); double getGPA(); void chngAddr (char addr[ ]); void chngAddr (char addr [ ], char city [ ]); }; * private: double GPA; double GPA; string address1, address2; string address1, address2; int num_of_grades; int num_of_grades; string fullName; string fullName;
11
Class Declaration Syntax class Name // usually capitalized { public: public members;// usually functions private: private members;// usually variables }; Note *
12
A Few Terms - class - class member - class object (class instance) - client
13
Objects An object is an instance of a class. Similarity: int row; This creates an instance of int called “row”. *
14
Objects If the class is Student, the objects may be the individual student names, the student GPAs, the student courses, etc. Alfred E. Newman is an instance of the class Student. CSE100 could be an array of 130 students and would be another instance of Student. *
15
Objects jane.addGrade(93.4); yoko.PrintGrade(); elvira.getGPA(); * * * CSE100[47].chngAddr(“Dakoda #4”); maribell.chngAddr(“123 Gnu”, “Chili, NY”);
16
Class Declarations * public: private: double GPA; string address1, address2; int num_of_grades; string fullName; cout << iago.GPA; cout << elvira. num_of_grades;
17
Class Declarations class Student { public: int ID; private: }; * cout << iago.ID; cout << elvira.ID; cout << CSE100[37].ID;
18
hall Objects and Members prIvate public PrintGradefunction code addGradefunction code getGPAfunction code chngAddrfunction code GPA2.81 address13 Ayn address215 Zola num_of_grades131 fullNameAl Hallsand PrintGradefunction code addGradefunction code getGPAfunction code chngAddrfunction code GPA3.45 address117 Emil address29 Gault num_of_grades93 fullNameJo Sand *
19
What can you do with a class? - declare as many objects as you like - pass class objects as parameters in a function and return them as function values - may be automatic
20
- use most built-in operataions add two objects hall + rand compare for equality hall == rand What you cannot do? *
21
What you can do? - work with individual members of a class hall.GPA + sand.GPA * hall.GPA == sand.GPA hall.GPA == sand.GPA hall.GPA = sand.GPA hall.GPA = sand.GPA hall = sand// member-by-member // assignment hall = sand// member-by-member // assignment
22
What you can do? - valid operators member selection. hall.getGPA member selection. hall.getGPA assignment = hall.GPA = sand.GPA assignment = hall.GPA = sand.GPA
23
Part of Some Client Code: Student hall; // declaration of an object of type Student Student sand; // declaration of an object of type Student double inGrade; // declaration of an object of type double hall.addGrade(84.3); cout > inGrade; sand.addGrade(inGrade); sand = hall;// member-by- member assigning sand.PrintGrade();// the grade will be 84.3 * * *
24
Class Scope declaration function call Student presley;presley.getGPA(); SomeClass someObjsomeObj.getGPA(); double xyz;getGPA();
25
Data Hiding data public class members
26
Files ª specification ª implementation ª client
27
Files client.cppimplementation.cpp #include “specification.h” specification.h void main()
28
Specification File class Name // usually capitalized { public: public members;// usually functions private: private members;// usually variables };
29
Sepcification File(s) #include “bool.h” class TimeType {public: void Set(int hours, int minutes, int seconds); void Increment(); void write() const; Boolean Equal(TimeType otherTime) Boolean LessThan(TimeType otherTime) private: int hrs; int mins; int secs; }; This is timetype.h
30
Implementation File(s) #include "timetype.h" #include void TimeType::Set( /* in */ int hours, /* in */ int minutes, /* in */ int seconds ) /* Precondition:Postcondition: 0 <= hours <=23 hrs == hours && 0 <= minutes <=59 && mins == minutes && 0 <= seconds <=59 && secs == seconds NOTE: This function MUST be called prior to any of the other member functions */ {hrs = hours; mins = minutes; secs = seconds; } This is timetype.cpp
31
Class Member Functions Syntax type Class_name::function_name(argument list) { function body } Example void TimeType::Set(int hour, int minu, int secon) { hrs = hour; mins = minu; secs = secon; } *
32
Client File(s) #include “timetype.h” #include void main() { TimeType appointment; appointment.Set(15, 30, 0); appointment.Write(); etc. } This is diary.cpp
33
Preprocessor Directives “filename” “path\filename”
34
Files diary.cpptimetype.cpp #include “timetype.h” timetype.h void main()
35
Multifile Program diary.exe linker timetype.h diary.cpptimetype.cpp diary.objtimetype.obj compiler
36
Multifile Program diary.exe linker timetype.h diary.cpptimetype.cpp diary.objtimetype.obj compiler datetype.obj compiler datetype.cpp
37
Managing Projects That Use Classes Place class declaration into an interface header file, classname.h. Place the implementation of the functions of this class into a separate implementation file, classname.cpp. Place the client file(s) in separate.cpp files, each of which should have #include”classname.h”. All client files and classname.cpp should be inserted as part of the project. * *
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.