280 likes | 403 Views
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
E N D
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
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
Data and Functions Associated Object Name
Data and Functions Associated Data (Attributes)
Data and Functions Associated Functions This is still a hypothetical car (a class) With no attributes defined
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 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
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
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.
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
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”; }
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”; }
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()
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()
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
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
Classes and Objects Data type (int) x y z int x, y, z;
Classes and Objects The Class (Car) racer roadster our_sample_car Car racer, roadster, our_sample_car; 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
Don’t you feel a bit more ‘Class’y ? Classes are fundamental to the Java Programming language and further programming in C++