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++ Class Why do we need Classes and Objects? Summary The Real World How do you look at things in the real world ? Objects Look at a car Wheels Chassis Steering Doors Color Model 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 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 Even Simpler: How about representing a balloon? Define it: radius we’ll just use this shape color can inflate it can pop it radius 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 How would you define it? Balloon initialize inflate pop display radius Data and Functions Associated Balloon initialize inflate pop display radius Class Name Data and Functions Associated Balloon initialize inflate pop display radius Functions Data and Functions Associated Balloon initialize inflate pop display radius A class is a schematic for a hypothetical balloon Attributes (the data) An Actual Balloon has attributes Balloon initialize inflate pop display radius = 5 We can use the radius to indicate whether balloon has been popped (by setting it to -1) Attribute (the size of the balloon) 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 Modeling a Balloon in C++ class Balloon { public: void initialize(int initRad); void inflate(int howMuch); void pop( ); Member Functions void display(); private: int radius; }; Member Data Modeling a Balloon in C++ class Balloon { public: void initialize(int initRad); void inflate(int howMuch); void pop( ); Users of Balloon void display(); can access private: int radius; }; Users of Balloon can’t access 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. Using a class -- objects When you declare a variable of your new type, it’s called an object Balloon hotAir; hotAir is an object Balloon is a class Balloon bal, weather; More objects You can use the public functions defined for the class Balloon bal; bal.initialize(5); bal.inflate(15); bal.pop(); Classes have Access Control Unlike struct, you can’t access the data directly (it’s private) Balloon hotAir; hotAir.radius=10; ILLEGAL You have to use the functions already defined in the class Balloon hotAir; hotAir.initialize(10); LEGAL 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 Implement the initialize and inflate functions void Balloon::initialize(int initRad) { This says it is a radius = initRad; member function of Class Balloon } void Balloon::inflate(int howMuch) { radius = radius + howMuch; Notice how the } parameter modifies the member data Implement the pop and display functions void Balloon::pop() { cout<<"Pop!“<<endl; radius = -1; } A “sentinel” value Meaning it’s popped void Balloon::display() { cout<<"("<<radius<<")"<<endl; } 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(); } 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) 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 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 Classes and Objects Data type (int) x y int x, y, z; z Classes and Objects The Class (Balloon) bal hotAir weather Balloon bal, hotAir, weather; Each object can have its own attributes 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 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 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 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; }; 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(); }; 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; }; Don’t you feel a bit more ‘Class’y ? Classes are fundamental to the Java Programming language and further programming in C++
© Copyright 2025 Paperzz