Cpp - class class Introduction

What Is a Type?

A C++ type defines data and its operations.

C++ has built-in types such as int, long, and float numbers.

For example, for unsigned short integers, each variable can hold a number between 0 and 65,535.

As for the operation, short integers can be added together, can do the math operations.

You can create your own types. A class is a definition of a new type.

Classes and Members

A C++ class is a template to create objects.

A class is a collection of related variables and functions bundled together.

Variables make up the data in the class, and functions perform tasks using that data.

After you define a class, objects are created from the class.

Declaring a Class

To declare a class, use the class keyword followed by the member variables and member functions of the class.

Here's an example for a Cart class:

class Cart 
    int speed; 
    int wheelSize; 

This code creates a class called Cart with two member variables, speed and wheelSize, and two member functions, pedal() and brake().

All four can be used by other classes because the public keyword precedes them in the class definition.

Defining an Object

An object is created from a class by specifying its class and a variable name. For example:

Cart shoppingCart; 

This statement creates a Cart object named shoppingCart.

The Cart class is used as a template for the object.

The object will have all member variables and member functions defined for the class.

Related Topics