Intro to Classes Chapter 12

Preview:

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

Intro to ClassesChapter 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

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

ObjectsLook at a car

WheelsChassisSteeringDoorsColorModel

The Car as an objectDefine it

4 WheelsMetal ChassisCan move left, right, forward and back2 DoorsBright Red ColorBMW Z3

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

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

How would you define it?

Car

Number wheelsMaterial chassisNumber doorsBody colorMake modelActions steering()

Data and Functions Associated

Car

Number wheelsMaterial chassisNumber doorsBody colorMake modelActions steering()

Object Name

Data and Functions Associated

Car

Number wheelsMaterial chassisNumber doorsBody colorMake modelActions steering()

Data

(Attributes)

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

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

}

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

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

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

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”;

}

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”;

}

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

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

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

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

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 WorldDefining a Class – Data and functionsOur first C++ ClassWhy do we need Classes and Objects?Summary

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

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

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

Classes are fundamental to the Java Programming language

and further programming in C++