Download presentation
Presentation is loading. Please wait.
Published byAubrie Golden Modified over 9 years ago
1
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C struct only permitted to have data members Functions to operate on such data appeared elsewhere Association with data depended upon programmer C++ struct may have function members as well Common package combines data and functions Combination of data and functions treated as single object
2
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 2 - C++ Classes Classes Word class arose in Simula 67 used to describe new data type Means in an object oriented language for Information hiding Abstraction Encapsulation
3
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 3 - C++ Classes Classes A C++ class mCollects entities with common characteristics into objects mIs a data type mCollection of data elements Data Members mSet of operations Function Members
4
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 4 - C++ Classes Classes class Name { body; }; class Vehicle { … }; Vehicle myVehicle; Vehicle * rentalVehicle = &my Vehicle;
5
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 5 - C++ Classes Classes Class Name Identifies the class Upper case by convention Helps to distinguish from variable or constant Instance Name Written in lower case Class Body Enclosed in curly braces Contains…. Data Members Function Members Terminated with a ;
6
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 6 - C++ Classes Class Access public Access Visible to general public Any function or class within the system Has access All data and function members that follow Available to everyone to use or modify
7
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 7 - C++ Classes Class Access protected Access Visible to Class members Classes named as friends Members of derived classes All data and function members that follow…... Appear private to outsiders Appear public to members and members of derived classes
8
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 8 - C++ Classes Class Access private Access More restrictive Visible to Class members Classes named as friends All data and function members that follow Hidden from everyone No one can change from outside the struct or class
9
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 9 - C++ Classes Class Access Access Restriction Increasing Class privateprotectedpublic Message Object
10
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 10 - C++ Classes Classes Public Interface Establish well defined persistent public interface Using access specifiers Hide implementation of the object Implementation Private and protected members can change Functionality remains constant
11
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 11 - C++ Classes Classes Declaration class Name { public:..… protected:..… private:..… }; private by default
12
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 12 - C++ Classes Classes Class Beers Instance myBeer Data members Each new instance of a class gets distinct copies of all data members Function Members Shared among all instances
13
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 13 - C++ Classes Classes Declaration class Name { public: function members data members protected: function members data members private: function members data members };
14
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 14 - C++ Classes Class Initialization - Constructors and Destructors Constructor Member function with the same name as the class Responsible for initializing class data members Can be overloaded to have zero or more parameters syntax className (arg0, arg1,... argn) { body } args are optional
15
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 15 - C++ Classes Class Initialization - Constructors The constructor is invoked…. Whenever class object declared At that time full type checking of the definition is applied Assumes level of accessibility of section in which declared Public Protected Private Invocation of new invokes the constructor for class instance If new fails the constructor not invoked What about malloc?
16
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 16 - C++ Classes Class Constructors class SimpleClass { public: SimpleClass () : myValue(10); SimpleClass (int aValue) : myValue(aValue); private: int myValue; }; int main (void} { SimpleClass myClass(15), yourClass; return 0; }
17
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 17 - C++ Classes Class Initialization - Constructors and Destructors Destructor Is member function Has the same name as the class Is preceded by the ~ symbol Provides means to de-initialize data members Has no parameters Cannot specify Return type or value Cannot be overloaded
18
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 18 - C++ Classes Class Initialization - Constructors and Destructors Destructor Storage to be de-initialized must have been allocated by new Can be called explicitly to clean up without deallocating syntax ~className ( ) { body } no args
19
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 19 - C++ Classes Class Initialization - Constructors and Destructors Destructor Destructor invoked Whenever delete operator applied to an object. Called by delete before freeing storage. Class object exits scope. Just before program terminates. What about free?
20
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 20 - C++ Classes Private Constructors... class Date { friend class Holiday; public: …. private: Date (int aDay=15, int aMonth=8, int aYear=1971); int day, month, year; }; class Holiday { public: Date *createHoliday(int day, int month) { myHoliday = new Date (day, month); return myHoliday; } private: Date *myHoliday; };
21
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 21 - C++ Classes Class Initialization - Constructors and Destructor Important to remember…... Constructor Initializes storage Does not allocate Destructor Deinitializes storage Does not delete Sequence new constructor destructor delete
22
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 22 - C++ Classes Class Member Functions Manager Implementer Helper Access
23
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 23 - C++ Classes Class Member Functions Manager Functions Manage the class objects Initialization / deinitialization Assignment Memory management Type conversion Examples of Manager Functions Constructor / destructor Invoked Explicitly by the compiler
24
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 24 - C++ Classes Class Member Functions Implementer Functions iDefine and provide Capabilities associated with class iInvoked Explicitly by the programmer iProvide Significant portion of the public interface to the class
25
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 25 - C++ Classes Class Member Functions class Stack { public: Stack() { head = NULL; } void push (Entry aValue) { add the value and set new head} Entry pop() { set new head and return value} private: Entry* head; };
26
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 26 - C++ Classes Class Member Functions Helper Functions oCarry out auxiliary tasks oNot invoked (typically) Explicitly by programmer oTypically dependent upon the underlying data structure oGenerally declared private
27
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 27 - C++ Classes Class Member Functions Access Functions Provide access to otherwise protected or private data Invoked Explicitly by the programmer Provide Remaining component of the public interface
28
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 28 - C++ Classes Classes - Overloading Class Member Functions Class Member Functions can use Same name In same scope If signature is unique Same rules as non-member functions applied to disambiguate Must provide implementation for each overloaded function
29
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 29 - C++ Classes Classes - Name Scope In C++ Have 3 kinds of scope…. u File u Local u Class
30
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 30 - C++ Classes Classes - Name Scope File Portion of the program not contained within the definition of Function Class Outer most scope of the program Encloses Local Scope Class
31
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 31 - C++ Classes Classes - Name Scope Local FPortion of the program (in general) Contained within the definition of a function FEach function Represents distinct local scope FWithin a function Each block maintains an associated local scope Those variables are local to the enclosing scope FLocal scopes can be nested
32
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 32 - C++ Classes Classes - Name Scope Class Each class represents a distinct scope Member functions Treated as within the scope of their class
33
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 33 - C++ Classes Classes - Unions Union syntax is based upon that for structures and classes syntax union Name { type1 var1; type2 var2;... typen varn; }; Name - union variable name serves as the type specifier typei - any of the types vari - variable of typei
34
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 34 - C++ Classes Classes - Unions In comparison with a class….. Union Can Only hold one type of variable at a time Declare Constructor Destructor Union Cannot Declare static data members Declare as a member instance of a class that defines a Constructor Destructor
35
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 35 - C++ Classes Classes - Unions union Mine { int intValue; float floatValue; char charValue; }; Mine aMine; char int float
36
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 36 - C++ Classes Classes - Unions Member Access….. Access union member exactly like struct or class unionName.member unionPtr -> member Example Mine me; me.intVal = 10;// me will hold an int me.charVal = ‘a’;// me will hold a char Members can be public - like a struct is the default protected private
37
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 37 - C++ Classes Classes - Unions Initialization Union can be initialized only with value of type of first member Anonymous Union Has no name tag Not followed by object definition Cannot have private or protected members Cannot have function members If defined at file scope … Must be declared static
38
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 38 - C++ Classes Summary Now have defined 3 variations on the basic class data structure [:baseList] { }; classKeyclass, struct, union classNamename of the class baseClasslist of classes the class is derived from member listdata and function members
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.