intro to classes chapter 12
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/1.jpg)
Intro to ClassesChapter 12
![Page 2: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/2.jpg)
Agenda
Classes – getting the Real World onto the Virtual World Defining a Class – Data and functionsOur first C++ ClassWhy do we need Classes and Objects?Summary
![Page 3: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/3.jpg)
The Real WorldHow do you look at things in the real world ?
ObjectsLook at a car
WheelsChassisSteeringDoorsColorModel
![Page 4: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/4.jpg)
The Car as an objectDefine it
4 WheelsMetal ChassisCan move left, right, forward and back2 DoorsBright Red ColorBMW Z3
![Page 5: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/5.jpg)
The Virtual WorldWhy make any difference in the Virtual World ?With C++ Classes and Objects this can be a realitySolve problems as you visualize them
![Page 6: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/6.jpg)
Agenda
Classes – getting the Real World onto the Virtual WorldDefining a Class – Data and Functions Our first C++ ClassWhy do we need Classes and Objects?Summary
![Page 7: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/7.jpg)
How would you define it?
Car
Number wheelsMaterial chassisNumber doorsBody colorMake modelActions steering()
![Page 8: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/8.jpg)
Data and Functions Associated
Car
Number wheelsMaterial chassisNumber doorsBody colorMake modelActions steering()
Object Name
![Page 9: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/9.jpg)
Data and Functions Associated
Car
Number wheelsMaterial chassisNumber doorsBody colorMake modelActions steering()
Data
(Attributes)
![Page 10: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/10.jpg)
Data and Functions Associated
Car
Number wheelsMaterial chassisNumber doorsBody colorMake modelActions steering()Functions
This is still a hypothetical car (a class)With no attributes defined
![Page 11: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/11.jpg)
A real car (object) has attributes
Car
Number wheels = 4;Material chassis = Metal;Number doors = 2;Body color = red;Make model = BMW Z3;Actions steering(Direction){
//move car in given Direction
}
![Page 12: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/12.jpg)
Agenda
Classes – getting the Real World onto the Virtual WorldDefining a Class – Data and functionsOur first C++ Class Why do we need Classes and Objects?Summary
![Page 13: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/13.jpg)
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
![Page 14: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/14.jpg)
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
![Page 15: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/15.jpg)
The C++ Classclass 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.
![Page 16: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/16.jpg)
Implement the steering functionvoid Car::steering(int direction){//let us assume direction = 1 means left, 2 = right, //3 = forward and 4 = backwardif ( 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
![Page 17: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/17.jpg)
Implement the printDetails function
void Car::printDetails(){//display the car characteristics to the screencout<<“The car is a “<<color<<“ “<<model
<<“ with “<<doors<<“ doors and a” <<chassis<<“ chassis”;
}
![Page 18: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/18.jpg)
Implement the setProperties functionvoid 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 variableswheels = wheel_number;chassis = chassis_type;doors = door_number;color = paint_color;model = car_model;
//display the car characteristics to the screencout<<“The car is a “<<color<<“ “<<model<<“ with “<<doors<<“ doors”;
}
![Page 19: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/19.jpg)
Make an instancevoid main(){
Car racer; racer.setProperties(4, “carbon”, 2, “white”, “porsche”);}
racer4
porschewhite
carbon 2wheels doors
color model
chassis
setProperties( )printDetails()steering()
The car is a white porsche with 2 doors
Console window
![Page 20: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/20.jpg)
Make another instancevoid main(){
Car roadster; roadster.setProperties(4, “steel”, 2, “red”, “mazda”);}
roadster4
mazdared
steel 2wheels doors
color model
chassis
setProperties( )printDetails()steering()
The car is a red mazda with 2 doors
Console window
![Page 21: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/21.jpg)
Entering details from keyboardvoid 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);
}
Make another instance
Input all the data
Set Properties to input data
![Page 22: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/22.jpg)
Voila – You have your first class !
Remember – the definition is called a classAn 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
![Page 23: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/23.jpg)
Classes and Objects
Data type
(int)
x y z
int x, y, z;
![Page 24: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/24.jpg)
Classes and Objects
The Class (Car)
racer roadster our_sample_car
Car racer, roadster, our_sample_car;
Each object can have its own attributes
![Page 25: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/25.jpg)
Agenda
Classes – getting the Real World onto the Virtual WorldDefining a Class – Data and functionsOur first C++ ClassWhy do we need Classes and Objects?Summary
![Page 26: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/26.jpg)
Why Classes and Objects ?It may seem overwhelming or unnecessary at firstAs the complexity/size of your code increases, classes will help you modularize your codeHelps you visualize and solve problems betterBrings more order into your code
![Page 27: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/27.jpg)
Agenda
Classes – getting the Real World onto the Virtual WorldDefining a Class – Data and functionsOur first C++ ClassWhy do we need Classes and Objects?Summary
![Page 28: Intro to Classes Chapter 12](https://reader035.vdocuments.mx/reader035/viewer/2022062520/5681634c550346895dd3e189/html5/thumbnails/28.jpg)
Don’t you feel a bit more ‘Class’y ?
Classes are fundamental to the Java Programming language
and further programming in C++