You are on page 1of 9

C++: Classes & Object Oriented Programming

Class and Object


Define a class
What is the relation between Class and Object?

Instantiate Objects
http://www.careerlink.vn/timviec-lam/programmingtraining-officer/369466

Class and Object


Class Name: Car Data Fields: Color is:___ Functions: getWidth() getHeight()

A class template

Car Object 1 Data Fields: Color is:_Blue__

Car Object 2 Data Fields: Color is:_Red__

Car Object 3 Data Fields: Color is:_Black__

Three objects of Car class

An object has both a state and behavior. The state defines the object, and the behavior defines what the object does.

Declaration: Class

Tells the compiler what member functions and data members belong to the class. Keyword class followed by the classs name. Class body is enclosed in braces ({}) Specifies data members and member functions Access-specifier public:

Indicates that a member function or data member is accessible to other functions and member functions of other classes.

Define Time Class (1/3)

Beginning of class definition for class Time

Whats the functions of public and private keyword?

Define Time Class (2/3)

Whats the functions of constructor?

Define Time Class (3/3)

Whats the functions of setfill and setw keyword?

Main Function
Use dot operator to call Times member function

Can we call t.hour = 13?

Example

Write a program that asks the user to input a name in the format last, first and outputs the name as first name, last name Sample run:

Input a name in the format Last, First: Minh, Phuong Output: Phuong Minh

You might also like