Learn C++ - C++ Custom Type






Defining Your Own Data Types

You can define a new data type by defining a class.

A class type can be a composite of variables of other types-of fundamental types or of other class types.

A class can also have functions as an integral part of its definition.

You could define a class type called Box that contains variables that store a length, a width, and a height to represent boxes.

You could then define variables of type Box, just as you define variables of fundamental types.

Each Box object would contain its own length, width and height dimensions and you could create and manipulate as many Box objects as you need in a program.

A class is a user-defined data type.

The variables and functions defined within a class are members of the class.

The variables are data members and the functions are function members.

The function members of a class are sometimes referred to as methods.

Variables of a class type store objects. Objects are sometimes called instances of the class.

Defining an instance of a class is referred to as instantiation.





Object-oriented

Object-oriented programming incorporates some additional important ideas (famously encapsulation and data hiding, inheritance, and polymorphism).

Inheritance is the ability to define one type in terms of another.

Polymorphism means the ability to assume different forms at different times.

Polymorphism in C++ always involves calling a function member of an object, using either a pointer or a reference.

Defining a Class

A class is a user-defined type.

The definition of a type uses the class keyword. The basic organization of a class definition looks like this:

  
class ClassName 
{ 
  // Code that defines the members of the class... 
}; 

The name of this class type is ClassName.

It's a common convention to use the uppercase name for user-defined classes to distinguish class types from variable names.

The members of the class are all specified between the braces.

The definitions for function members can be inside or outside the class definition.

   
class Box { 
private: 
  double length {1.0}; 
  double width {1.0}; 
  double height {1.0}; 
public: 
  // Function to calculate the volume of a box 
  double volume() { 
    return length*width*height; 
  } 
}; 

length, width, and height are data members of the Box class and are all of type double.

Every Box object will have its own set of data members.

This is obvious really-if they didn't have their own data members, all objects would be identical.

You could create a variable of type Box like this:

    
Box myBox; // A Box object with all dimensions 1 

The myBox variable refers to a Box object with the default data member values. You could call the volume() member for the object to calculate the volume:

  
std::cout << "Volume of myBox is" << myBox.volume() << std::endl;  // Volume is 1.0 

You could specify the data members as public, in which case you can set them explicitly from outside the class, like this:

    
myBox.length = 1.5; 
myBox.width = 2.0; 
myBox.height = 4.0; 
std::cout << "Volume of myBox is" << myBox.volume() << std::endl;  // Volume is 12.0