28
Intro to Classes Chapter 12

Intro to Classes Chapter 12

  • Upload
    donny

  • View
    21

  • Download
    0

Embed Size (px)

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 Presentation

Citation preview

Page 1: Intro to Classes Chapter 12

Intro to ClassesChapter 12

Page 2: Intro to Classes Chapter 12

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

The Real WorldHow do you look at things in the real world ?

ObjectsLook at a car

WheelsChassisSteeringDoorsColorModel

Page 4: Intro to Classes Chapter 12

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

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

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

How would you define it?

Car

Number wheelsMaterial chassisNumber doorsBody colorMake modelActions steering()

Page 8: Intro to Classes Chapter 12

Data and Functions Associated

Car

Number wheelsMaterial chassisNumber doorsBody colorMake modelActions steering()

Object Name

Page 9: Intro to Classes Chapter 12

Data and Functions Associated

Car

Number wheelsMaterial chassisNumber doorsBody colorMake modelActions steering()

Data

(Attributes)

Page 10: Intro to Classes Chapter 12

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

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

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

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

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

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

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

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

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

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

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

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

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

Classes and Objects

Data type

(int)

x y z

int x, y, z;

Page 24: Intro to Classes Chapter 12

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

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

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

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

Don’t you feel a bit more ‘Class’y ?

Classes are fundamental to the Java Programming language

and further programming in C++