New version page

Classes in C++

Upgrade to remove ads

This preview shows page 1-2-3 out of 10 pages.

Save
View Full Document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Upgrade to remove ads
Unformatted text preview:

Classes in C++C structsC++ structsC++ adds classMember functionsInheritance (class derivation)OOP = Data abstraction + inheritance + dynamic bindingSubclasses override virtual functionsCalling a virtual functionWhy dynamic binding?11Jan 13, 2019Jan 13, 2019 Classes in C++Classes in C++C++ originally called "C with classes":Swedish connection: Bjarne Stoustrup borrowed from Simula (’67)Simulating classes of real world objectsC++ continues to evolve:Version 1.0 released by AT&T in 1986Version 2.0 in 1990 (added multiple inheritance)Version 3.0 in 1992 (templates)ANSI standard in 1996 (exception handling, run time type info)C++ became dominant OOPL in early 90'sNow Java and C# challengeA class extends the C++ type system: class Account { //creates a new typeclass Account { //creates a new type}; //Note: you need this semi-colon in C++}; //Note: you need this semi-colon in C++Account a1,a2; //define variables of type AccountAccount a1,a2; //define variables of type Account22Jan 13, 2019Jan 13, 2019 C structsC structsA data structure for dates in C:A data structure for dates in C:struct Date {struct Date {int month, day, year;int month, day, year;};};int set(struct Date*, int m, int d, int y);int set(struct Date*, int m, int d, int y);int print(struct Date*);int print(struct Date*);What’s the problem?What’s the problem?No information hidingNo information hidingNo way to control access to data (obscure side effects)No way to control access to data (obscure side effects)No way to prevent assigning an illegal value to monthNo way to prevent assigning an illegal value to monthChanging representation of Date breaks all client codeChanging representation of Date breaks all client code33Jan 13, 2019Jan 13, 2019 C++ structsC++ structsC++ provides for closer coupling of data and functions:C++ provides for closer coupling of data and functions:struct Date {struct Date {int month, day, year;int month, day, year;void set(int m, int d, int y);void set(int m, int d, int y);void print(); //implement elsewherevoid print(); //implement elsewhere};};Invoke functions with variable . memberFunction():Invoke functions with variable . memberFunction():Date today; //In C++, structs automatically define typesDate today; //In C++, structs automatically define typestoday.set(9,29,1953); today.print();today.set(9,29,1953); today.print();Now we have Now we have data abstractiondata abstraction::ProceduralProcedural abstraction hides details of code in functions abstraction hides details of code in functionsDataData abstraction couples data structure and functions abstraction couples data structure and functionsStill, no information hiding: Still, no information hiding: today.set(50,-10,0);today.set(50,-10,0);44Jan 13, 2019Jan 13, 2019 C++ adds C++ adds classclassC++ adds new keywords to support information hiding:C++ adds new keywords to support information hiding:classclass Date { Date {int month, day, year;int month, day, year; public:public:void set(int m, int d, int y);void set(int m, int d, int y);void print(); //implement elsewherevoid print(); //implement elsewhere};};Members after Members after public:public: are visible to clients: are visible to clients:Date today; today.print();Date today; today.print();today.month = 50;today.month = 50; //is this legal? //is this legal?Members after Members after classclass are by default private are by default private55Jan 13, 2019Jan 13, 2019 Member functionsMember functionsWhat can we add to What can we add to DateDate to allow access to to allow access to monthmonth??class Date {class Date {int month, day, year;int month, day, year; public:public:void set(int m, int d, int y);void set(int m, int d, int y);void print(); //implement elsewherevoid print(); //implement elsewhereint getMonth() { return month; }int getMonth() { return month; }};}; cout << today.getMonth(); //outside class Datecout << today.getMonth(); //outside class DatePros and cons of this approach?Pros and cons of this approach?inline function is efficient, though it tends to break information hidinginline function is efficient, though it tends to break information hidingLet’s define Let’s define set()set() and protects the and protects the monthmonth data: data:void Date :: set(int m, int d, int y)void Date :: set(int m, int d, int y){ assert(m >= 1 && m <= 31);{ assert(m >= 1 && m <= 31); month = m;month = m;}}66Jan 13, 2019Jan 13, 2019 Inheritance Inheritance (class derivation)(class derivation)class class AccountAccount generalizes many kinds of bank generalizes many kinds of bank accounts: checking, savings, etc.accounts: checking, savings, etc.C++ class derivation captures this generation:C++ class derivation captures this generation:class Checking : public Account {class Checking : public Account { public:public:Checking(float balance);Checking(float balance);Checking(); //default constructorChecking(); //default constructor};};: : denotes derivation—denotes derivation—CheckingChecking inherits from inherits from AccountAccountpublicpublic derivation denotes subtype inheritancederivation denotes subtype inheritanceAccountAccount’s public methods accessible to instances of ’s public methods accessible to instances of CheckingCheckingChecking myChecking(200);Checking myChecking(200);//Checking constructor//Checking constructormyChecking.getBalance();myChecking.getBalance();//Account function//Account function77Jan 13, 2019Jan 13, 2019 OOP = Data abstraction OOP = Data abstraction + inheritance + inheritance + dynamic binding+ dynamic bindingPolymorphism: a function can mean different things at runtimePolymorphism: a function can mean different things at runtimeDynamic binding: defer function binding to a subtype until runtimeDynamic binding: defer function binding to a subtype until runtimeSuppose we want to draw a heterogeneous collection of shapes?Suppose we want to draw a heterogeneous collection of shapes?class Point { ... }; //a Point has x and y coordinatesclass Point { ... }; //a Point has x and y coordinatesclass Shape {class Shape { protected:protected://accessible to subclasses but otherwise private//accessible to subclasses but otherwise privatePoint center; //all Shapes have a center PointPoint center; //all Shapes have a center Point public:public:Point where() { return center; } Point where() { return center; } virtualvirtual void move(Point to) //virtual can be overridden


Download Classes in C++
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Classes in C++ and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Classes in C++ 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?