17
Definition Class In C++, the class is the construct primarily used to create objects.

Definition Class In C++, the class is the construct primarily used to create objects

Embed Size (px)

Citation preview

Page 1: Definition Class In C++, the class is the construct primarily used to create objects

Definition

ClassIn C++, the class is the construct primarily used to create objects.

Page 2: Definition Class In C++, the class is the construct primarily used to create objects

Definition

OBJECT

Objects are instances of a class.

Page 3: Definition Class In C++, the class is the construct primarily used to create objects

Definition

Encapsulation

Page 4: Definition Class In C++, the class is the construct primarily used to create objects

Definition

Constructor

A constructor is a member function that is automatically called when a class object is created.

Default constructorA constructor that has no parameters.

Page 5: Definition Class In C++, the class is the construct primarily used to create objects

Definition

Friend of classes

A friend is a function that is not a member of a class, but has access to the private members of the class.

Page 6: Definition Class In C++, the class is the construct primarily used to create objects

Definition

Memberwise Assignment

The = operator may be used to assign one object to another or to initialize the object with another object’s data. By default, each member of one object is copied to its counterpart in the other object.

Page 7: Definition Class In C++, the class is the construct primarily used to create objects

Definition

Copy Constructor

A copy constructor is a special constructor that is called whenever a new object is created and initialized with the data of another object of the same class

Ex: Car (Car a) { myYear = a.getYear();

myMake = a.getMake();}

Page 8: Definition Class In C++, the class is the construct primarily used to create objects

Which of the following shows the correct use of the scope resolution operator in a member function definition?

• InvItem:: void setOnHand (int units)

• Void InvItem:: setOnHand (int units)

Page 9: Definition Class In C++, the class is the construct primarily used to create objects

Assuming that soap is an instance of an Inventory class, which of the following is a valid call to the setOnHand member functionInventory soap;

setOnHand(20);soap::setOnHand(20);soap.setOnHand(20);Inventory.setOnHand(20);

Page 10: Definition Class In C++, the class is the construct primarily used to create objects

A private class member function can be called by

• Any other function• Only public functions in the same class• Only private functions the same class• Any function in the same class

Page 11: Definition Class In C++, the class is the construct primarily used to create objects

Assume a map class has a member variable named position that is an instance of the Location class. The Location class has a private member variable named latitude and a public member function called getLatitude.How would you return the value stored in latitude• return Location.latitude();• return Location.getLatitude();• return position.latitude();• return position.latitude();

Page 12: Definition Class In C++, the class is the construct primarily used to create objects

Assume there is a class named Pet. Write the prototype for a member of Pet that overloads the = operator.

Pet Pet :: operator= (Pet);

Page 13: Definition Class In C++, the class is the construct primarily used to create objects

What is the output for the following:

try {….throw (99);…

} catch (int e) { cout << “The exception caught is : “ << e << endl; }

cout << “ The code ran without exception “ << endl;

Page 14: Definition Class In C++, the class is the construct primarily used to create objects

What is the output for the following:

try {….throw “ Bad input”;…

} catch (int e) { cout << “The exception caught is : “ << e << endl; }

cout << “ The code ran without exception “ << endl;

Page 15: Definition Class In C++, the class is the construct primarily used to create objects

What is the output for the following:

try {….throw “ Bad input”;…

} catch (int e) { cout << “The exception caught is : “ << e << endl; }

catch (string se){ cout << The exception caught is “ << se << endl;}

cout << “ The code ran without exception “ << endl;

Page 16: Definition Class In C++, the class is the construct primarily used to create objects

What is the output for the following:

try {….throw (99);throw “ Bad input”;…

} catch (int e) { cout << “The exception caught is : “ << e << endl; }

catch (string se){ cout << The exception caught is “ << se << endl;}

cout << “ The code ran without exception “ << endl;

Page 17: Definition Class In C++, the class is the construct primarily used to create objects

Complete the following code skeleton to declare a class called DATE. The class should contain member variables and functions to store and retrieve the month, day, and year components of a date

Class Date {private: public:}