Home

Introduction to Classes

 

Introduction

In the previous lesson, we introduced data types that are usually referred to as primitive. Each of these data types is used to create an object that can hold a single value, such as the number of bedrooms of a house, the type of house, etc. Here are examples of such variables:

using namespace System;

int main()
{
	__wchar_t TypeOfHome;
	int Bedrooms;
	Byte Stories;
	double Bathrooms;
	int YearBuilt;
	double Value;

	return 0;
}

Notice that, in this example, some or all of the variables could be used to create a single object.

Practical LearningPractical Learning: Introducing Classes

  1. To start a new program, launch Microsoft Visual C++ 2005
  2. On the main menu, click File -> New -> Project...
  3. On the left side, make sure that Visual C++ is selected. In the Templates list, click CLR Empty Project
  4. In the Name box, replace the string with RealEstate4 and click OK
  5. To create a source file, on the main menu, click Project -> Add New Item...
  6. In the Templates list, click Source File (.cpp)
  7. In the New box, type Exercise and click Add
  8. In the empty file, type:
     
    using namespace System;
    
    int main()
    {
        Console::WriteLine("=//= Altair Realty =//=");
        Console::WriteLine("-=- Properties Inventory -=-");
        return 0;
    }
  9. To execute the application, on the main menu, click Debug -> Start Without Debugging
  10. Click Yes

Creating a Class

The C++ language allows you to use one, or a group of more than one, type of values to create what would be referred to as a composite type. This type is created as a class. As introduced in Lesson 1, to create a class, start with either the struct or the class keyword, followed by a name and ending with a semi-colon. The name of the class follows the same rules we have been applying to the variables so far. Here is an example:

struct House;

Or

class House;

We we will start the names of our classes with C:

struct CHouse;

Or

class CHouse;

After creating a CHouse class like this, it doesn't (yet) have any functionality. You have simply indicated that you want a data type that is itself one or a group of values. To create it, you list the parts of a class between an opening curly bracket "{" and a closing curly bracket "}". Everything between these brackets is part of the class and such a section is referred to as the body of the class. It would be created as follows:

House
struct CHouse{ };

Or

class CHouse{ };

In the body of the class, you can list the items that compose it. Each item is called a member of the class. Each item is identified by its type and a name. For example, if you want to create a CHouse class that is simply known for its value, you can define a member value of double type. Such a class would look like this:

struct CHouse{ double Value; };

Or

class CHouse{ double Value; };

Most objects are made of various parts. Listing all parts on a single line could be cumbersome. Therefore, you can span the body of a class on various lines. An example would be:

struct CHouse {
// Body of the class
};

or

struc CHouse
{
// Body of the class
};

Or

class CHouse {
// Body of the class
};

or

class CHouse
{
// Body of the class
};

In the body of the class, you can then list its members. Each member is created as a variable, declaring it using any of the techniques we used in the previous lesson. Here are examples:
struct CHouse
{
	__wchar_t TypeOfHome;
	int Bedrooms;
	Byte Stories;
	double Bathrooms;
	int YearBuilt;
	double Value;
};
class CHouse
{
	__wchar_t TypeOfHome;
	int Bedrooms;
	Byte Stories;
	double Bathrooms;
	int YearBuilt;
	double Value;
};

If two variables are of the same type, you can declare them with one data type as done for variable declarations in the previous lesson. Here is an example:

struct CHouse
{
	__wchar_t TypeOfHome;
	int Bedrooms, YearBuilt;
	Byte Stories;
	double Bathrooms, Value;
};
class CHouse
{
	__wchar_t TypeOfHome;
	int Bedrooms, YearBuilt;
	Byte Stories;
	double Bathrooms, Value;
};

To indicate that a member variable belongs to the class, another way you can declare it is by preceding its name with the name of the class followed by the :: operator. Here are example:

struct CHouse
{
	__wchar_t CHouse::TypeOfHome;
	int CHouse::Bedrooms;
	Byte CHouse::Stories;
	double CHouse::Bathrooms;
	int CHouse::YearBuilt;
	double CHouse::Value;
};
class CHouse
{
	__wchar_t TypeOfHome;
	int CHouse::Bedrooms, YearBuilt;
	Byte Stories;
	double Bathrooms, CHouse::Value;
};
 

Practical LearningPractical Learning: Creating a Class

  1. On the main menu, click Project -> Add New Item...
  2. In the Templates list, click Header File (.h)
  3. In the New box, type Property and click Add
  4. In the empty file, type the following:
     
    #pragma once
    
    class CHouse
    {
        long PropertyNumber;
        __wchar_t PropertyType;
        unsigned Stories;
        unsigned Bedrooms;
        float Bathrooms;
        unsigned YearBuilt;
        double PropertyValue;
    };
  5. Save the file
 

Previous Copyright © 2006-2016, FunctionX, Inc. Next