Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World  Defining a Class – Data and functions Our first C++

Similar presentations


Presentation on theme: "Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World  Defining a Class – Data and functions Our first C++"— Presentation transcript:

1 Intro to Classes Chapter 18 AND 19

2 Agenda Classes – getting the Real World onto the Virtual World  Defining a Class – Data and functions Our first C++ Class Why do we need Classes and Objects? Summary

3 The Real World How do you look at things in the real world ? Objects Look at a car Wheels Chassis Steering Doors Color Model

4 The Car as an object Define it 4 Wheels Metal Chassis Can move left, right, forward and back 2 Doors Bright Red Color BMW Z3

5 The Virtual World Why make any difference in the Virtual World ? With C++ Classes and Objects this can be a reality Solve problems as you visualize them

6 Even Simpler: How about representing a balloon? Define it: radius  we’ll just use this shape color can inflate it can pop it radius

7 Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and Functions  Our first C++ Class Why do we need Classes and Objects? Summary

8 How would you define it? Balloon initialize inflate pop display radius

9 Data and Functions Associated Class Name Balloon initialize inflate pop display radius

10 Data and Functions Associated Functions Balloon initialize inflate pop display radius

11 Data and Functions Associated Attributes (the data) Balloon initialize inflate pop display radius A class is a schematic for a hypothetical balloon

12 An Actual Balloon has attributes Attribute (the size of the balloon) Balloon initialize inflate pop display radius = 5 We can use the radius to indicate whether balloon has been popped (by setting it to -1)

13 Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++ Class  Why do we need Classes and Objects? Summary

14 Modeling a Balloon in C++ class Balloon { public: void initialize(int initRad); void inflate(int howMuch); void pop( ); void display(); private: int radius; }; Member Data Member Functions

15 Modeling a Balloon in C++ class Balloon { public: void initialize(int initRad); void inflate(int howMuch); void pop( ); void display(); private: int radius; }; Users of Balloon can’t access Users of Balloon can access

16 The C++ Class class Balloon { public: void initialize(int initRad); void inflate(int howMuch); void pop( ); void display(); private: int radius; }; A class defines the data and the functions that operate on the data A class is like defining your own data type, with associated functions which can act on objects of your type.

17 Using a class -- objects When you declare a variable of your new type, it’s called an object Balloon hotAir; Balloon bal, weather; hotAir is an object Balloon is a class More objects

18 You can use the public functions defined for the class Balloon bal; bal.initialize(5); bal.inflate(15); bal.pop();

19 Classes have Access Control Unlike struct, you can’t access the data directly (it’s private) You have to use the functions already defined in the class Balloon hotAir; hotAir.radius=10; ILLEGAL Balloon hotAir; hotAir.initialize(10); LEGAL

20 Why the extra restrictions? For many objects it’s too dangerous to allow ignorant (or malicious) users the ability to modify the data in an un-authorized manner Like encasing a complicated device (your iPod) in a protective package—opening package voids the warranty You can only play, download, select song (functions) We put “walls” around the object so it acts more thing-like…that’s why the keyword private

21 Implement the initialize and inflate functions void Balloon::initialize(int initRad) { radius = initRad; } void Balloon::inflate(int howMuch) { radius = radius + howMuch; } This says it is a member function of Class Balloon Notice how the parameter modifies the member data

22 Implement the pop and display functions void Balloon::pop() { cout<<"Pop!“<<endl; radius = -1; } void Balloon::display() { cout<<"("<<radius<<")"<<endl; } A “sentinel” value Meaning it’s popped

23 A ‘client’ program is one that uses a class int main() { Balloon myBalloon; myBalloon.initialize(3); cout<<"myBalloon currently has Radius "; myBalloon.display(); cout<<"\nInflating myBalloon by 8 \n"; myBalloon.inflate(8); cout<<"Now myBalloon has Radius "; myBalloon.display(); cout<<"\nPopping myBalloon \n"; myBalloon.pop(); cout<<"myBalloon currently has Radius "; myBalloon.display(); }

24 Results when executing previous program: myBalloon currently has Radius (3) Inflating myBalloon by 8 Now myBalloon has Radius (11) Popping myBalloon Pop! myBalloon currently has Radius (-1)

25 Improvements to Balloon functions We can model the balloon better: If the balloon is already popped, you can’t inflate it If you inflate to a radius over 25, balloon pops void Balloon::inflate(int howMuch) { if (radius >= 0) radius = radius + howMuch; if (radius > 25) pop(); } Invokes a different member function

26 Voila – You have your first class ! Remember – the definition is called a class An instance of a class is called an object Example: int y; Here int is the type– is analogous to a class y is the instance of the type and is analogous to an object

27 Classes and Objects Data type (int) xyz int x, y, z;

28 Classes and Objects The Class (Balloon) balhotAirweather Balloon bal, hotAir, weather; Each object can have its own attributes

29 Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++ Class Why do we need Classes and Objects?  Summary

30 Why Classes and Objects ? It may seem overwhelming or unnecessary at first As the complexity/size of your code increases, classes will help you modularize your code Helps you visualize and solve problems better Brings more order into your code

31 Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++ Class Why do we need Classes and Objects? Summary

32 The full Balloon class definition class Balloon { public: void initialize(int initRad); void inflate(int howMuch); void pop( ); void display(); void input(); //reads data like (10) from keybd float volume(); // returns volume of balloon private: int radius; };

33 You will also work with a (buggy) Time class class Time { public: // for public or client use void set(int h, int m, char mrd); void advance(int h, int m); void display(); private: // for internal use int hr, min; // the hour and minute char merid; // meridian: a(m) or p(m) void validate(); };

34 And complete the implementation of an Accounts class (don’t use on Project 2) class Account { public: // for public or client use void initialize(string newName, int newID, int newPIN, float newBalance ); void deposit(float money); void withdraw(int money); void display(); private: // for internal use string name; int userID, PIN; float balance; };

35 Don’t you feel a bit more ‘Class’y ? Classes are fundamental to the Java Programming language and further programming in C++


Download ppt "Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World  Defining a Class – Data and functions Our first C++"

Similar presentations


Ads by Google