1 / 28

Intro to Classes Chapter 12

Intro to Classes Chapter 12. 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

donny
Télécharger la présentation

Intro to Classes Chapter 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Intro to ClassesChapter 12

  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. 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

  7. How would you define it?

  8. Data and Functions Associated Object Name

  9. Data and Functions Associated Data (Attributes)

  10. Data and Functions Associated Functions This is still a hypothetical car (a class) With no attributes defined

  11. A real car (object) has attributes

  12. 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

  13. Modeling a car in C++ class Car { private: int wheels; string chassis; int doors; string color; string model; public: void steering(int direction); void setProperties(int, string, int, string, string); void printDetails(); }; Member Data Member Functions

  14. Modeling a car in C++ class Car { private: int wheels; string chassis; int doors; string color; string model; public: void steering(int direction); void setProperties(int, string, int, string, string); void printDetails(); }; Ignore these for now

  15. The C++ Class • class Car • { private: int wheels; string chassis; int doors; string color; string model; public: void steering(int direction); void setProperties(int, string, int, string, string); void printDetails(); • }; This contains the data and functions associated with the particular object we are trying to model. A Class is like defining your own data type, with associated functions which can act on objects of your type.

  16. Implement the steering function void Car::steering(int direction) { //let us assume direction = 1 means left, 2 = right, //3 = forward and 4 = backward if ( direction == 1) cout << “Moving car one unit left”; else if ( direction == 2) cout << “Moving car one unit right”; else if ( direction == 3) cout << “Moving car one unit forward”; else if ( direction == 4) cout << “Moving car one unit backward”; } This says it is a member function of Class Car

  17. Implement the printDetails function void Car::printDetails() { //display the car characteristics to the screen cout<<“The car is a “<<color<<“ “<<model <<“ with “<<doors<<“ doors and a” <<chassis<<“ chassis”; }

  18. Implement the setProperties function void Car::setProperties(int wheel_number, string chassis_type, int door_number, string paint_color, string car_model) { //take in user entered parameters and assign it to // the member variables wheels = wheel_number; chassis = chassis_type; doors = door_number; color = paint_color; model = car_model; //display the car characteristics to the screen cout<<“The car is a “<<color<<“ “<<model<<“ with “<<doors<<“ doors”; }

  19. Make an instance void main() { Car racer; racer.setProperties(4, “carbon”, 2, “white”, “porsche”); } racer wheels 4 carbon chassis 2 doors Console window The car is a white porsche with 2 doors white color porsche model setProperties( ) printDetails() steering()

  20. Make another instance void main() { Car roadster; roadster.setProperties(4, “steel”, 2, “red”, “mazda”); } roadster wheels 4 steel chassis 2 doors Console window red color mazda model The car is a red mazda with 2 doors setProperties( ) printDetails() steering()

  21. Entering details from keyboard Make another instance void main() { Car our_sample_car; int wheel_number; string chassis_type; int door_number; string paint_color; string car_model; int direction; cout<<“Enter the number of wheels:”; cin>> wheel_number; cout<<“Enter the chassis type:”; cin>> chassis_type; cout<<“Enter the number of doors:”; cin>> door_number; cout<<“Enter the color of the car:”; cin>> paint_color; cout<<“Enter the car model:”; cin>> car_model; cout<<“What direction would you like the car to go ? (1 = left, 2 = right, 3 = forward, 4 = backward): ”; cin>> direction; our_sample_car.setProperties(wheel_number, chassis_type, door_number, paint_color, car_model); our_sample_car.steering(direction); } Input all the data Set Properties to input data

  22. 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 definition – is analogous to a class • y is the instance of the type and is analogous to an object

  23. Classes and Objects Data type (int) x y z int x, y, z;

  24. Classes and Objects The Class (Car) racer roadster our_sample_car Car racer, roadster, our_sample_car; Each object can have its own attributes

  25. 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

  26. 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

  27. 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

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

More Related