Weight and health related risks.


Try it and it's free. PC and mobile version can be found on my website Healthfirst.
Android native application, named BmiScores, can be found on Google paly Store.

 

Class Explained

Syntax

<
Keyword><class name>
{
class body
};




Let's apply the above syntax to create a basic class named vehicle. This should be done as follow:

class vehicle
{
  // vehicle class body goes here
};

you have just created your class. Now, let's take a closer look to the class features.


Where class is a reserved word that tells the compiler you declared a class (any class must start with the word class).
The class body is made up with
access specifiers, data members (variables) and function members.
Access specifiers come in three flavors:

1- Private:

The access is guaranteed only to the class members(member functions and static data) in
which they are declared as well as the class friends. These terms will be explained as we go
through this tutorial. This is the class default specifier.

2- Protected:

The access is guaranteed to the class members(member functions and static data) in
which they are declared as well as the class friends and member functions, static data and
friends of any derived class from the class in question.

3- Public:

The access is guaranteed from anywhere in the program to which is visible.

All the concepts will be illustrated through examples. Lets start with the following example.

Example:

class vehicle
{
private:
int wheels ;// number of wheels
int length; // vehicle length
int weight; // vehicle weight
int width ;// vehicle width

public:
void car_description(int roues, int longueur,int poids,int largeur)
{
wheels
= roues;
length
= longueur;
weight
= poids ;
width
= largeur;
}

void display_car_description()
{
cout
<< "This car has " <<wheels<< " Wheels" << endl;
cout
<< "This car has a length of " << length <<" meters"<<endl;
cout
<< " This car weighs " << weight << " kg" << endl;
cout
<< " This car has a width of " <<width << " feet"<<endl;
}
};


This class has four data members of type int (as I said at the beginning data members are variables) which are private and two member functions which are public.

In the above class, the data: wheels, length, weight and width are accessed or reached only via the member functions which are in this case the functions car_description and display_car_description.

The member functions car_description and display_car_description could have only their prototype inside the class and they can definitively be defined outside the class. To do so, a special syntax is needed. See below, the redefinition of the same class as the above which shows this concept. Before jumping to the example, the following is a general syntax for a such case.

<
return_data_type> <class_name>:: function_name(arguments)
{
function body;
}

Where return_data_type is a C++ valid data type(int, double, float, string, char and so on) that can be returned by the function. If the function does not return anything, then use void for return_data_type. class_name is the name you gave to your class in which you defined the member functions prototype. The function_name is any C++ valid name that was given to member function. The member functions can have none, one or more arguments.

The example below shows the definition of the member functions outside of the class in which they were created or defined.

class vehicle
{
private:
int wheels ;// number of wheels
int length; // vehicle length
int weight; // vehicle width
int width ;// vehicle width

public:
void car_description(int roues, int longueur,int poids,int largeur);
void display_car_description();
};

void vehicle:: car_description(int roues, int longueur,int poids,int largeur)
{
wheels
= roues;
length
= longueur;
weight
= poids ;
width
= largeur;
}

void vehicle:: display_car_description()
{
cout
<< "This car has " <<wheels<< " Wheels" << endl;
cout
<< "This car has a length of " << length <<" meters"<<endl;
cout
<< " This car weighs " << weight << " kg" << endl;
cout
<< " This car has a width of " <<width << " feet"<<endl;
}




C++ Constructors



Another type of functions member named constructors.The constructors are similar to the other member functions except that:

1- Constructors do not return a value or statements
2- Constructors have same name as the class name

The constructors are mostly used to initialize member data. Back to our famous class "vehicle". We will rewrite the class using constructors.

The following example uses the Constructors and defines them within the class.

class vehicle
{
private:
int wheels ;// number of wheels
int length; // vehicle length
int weight; // vehicle width
int width ;// vehicle width

public:
// Parameterless Default constructor initializes all the data member to value 0
vehicle():wheels(0),length(0),weight(0),width(0)
{ }

//Constructor with arguments
vehicle (int roues,int longueur,int poids,int largeur):wheels(roues),length(longueur),weight(poids),width(largeur)
{ }

void display_car_description(); // This is a member function which is defined as in the previous class.
};

The constructors when used inside the class, to assign data, have the following syntax:

<
constructor_name><arguments>:var1(val1), var2(val2),… //Note the colon

where constructor_name is the class name and arguments are val1, val2,…. the default constructor has no arguments.

To define the constructor outside the class in which is declared is done in same way as the definition of the member functions except the constructor has no return data or statements and has the class name.


A word about access specifiers


Consider the following class:



class person
{
private:
int age;
float weight;
char name[20];
public:
//The following functions need to be implemented or coded
void set_info(……)
void get_info()
};

This class has three data items declared as private and two member functions declared as public. The private specifier could be omitted. By default, the members of the class are private unless otherwise specified.

The above class can also be written as below:

class person
{
int age;
float weight;

public:
//The following functions need to be implemented or coded
void set_info(……)
void get_info()
private:
char name[20];
};

In this class, age and weight are private by default (note the omission of private access specifier) followed by two public member functions then followed by private data member name. In general, any data member or member functions that come after the access specifier hold the access assigned to it till the appearance of another access specifier as shown in the above class. In other words, the access specifiers can be mixed.

Next, I'll deal with inheritance and polymorphism.
To be continued