Start Programming in Visual C++ 6.0 - C++ code example.

By Sergey Skudaev

+++++

itzalist Computers Directory

Here you will learn how to use Visual Studio to write simple program in C++. C++ code example is provided. If you want to learn C++ you will need to use Borland C++ or MS Visual Studio, Besides, you can download Bloodshed Dev-C++ that is free software under the GNU General Public License

Install Visual studio 6.0. Open Windows Explorer and create a new folder VisualC. Inside the VisualC folder create a folder Hello. See Start Visual Basic tutorial for details.

Launch Visual C++ 6.0 Application. From main menu select File, New. In New project window type Project name: HelloWorld. Select Win32 Console Application. Click Location browser button and select folder Hello created earlier. Click OK

C++ tutorial: C++ Project
Hover mouse to enlarge image.

Select A Simple Win32 Application and click OK

C++ tutorial: Simple Aplication

HelloWorld.cpp file and stdafx.h file will be created by system.

// HelloWorld.cpp : Defines the entry point for the console application.

#include "stdafx.h"

int main(int argc, char* argv[])
{
return 0;
}

Select Insert, New Class from main menu.

C++ tutorial: Simple Aplication

The New Class window displays. Enter class name Greeting (it may be anything)

C++ tutorial: Class type and name

Click OK button. Greeting class will be created. Select ClassView tab.

C++ tutorial: Class View

Right click on Greeting class and select Add Member Variable.

C++ tutorial: Add member variable

In Add Member variable window, type "char*" in Variable Type field and type "message" in Variable name field. Select Private for access. Click OK

C++ tutorial: Variable Type

Select Greeting class again and click right mouse button. Select Add Member Function. Type "void" in Function Type field and type "PrintMessage" in Function Declaration field. Select public for access. Click OK.

C++ tutorial: Add Member Function

System created Greeting.h, Greeting.cpp files.

C++ tutorial: Workspace

Greeting.h

// Greeting.h: interface for the Greeting class.

//////////////////////////////////////////////////////////////////////

#if !defined(AFX_GREETING_H__34246B3E_5382_401E_9265_7B6DFEFA39AF__INCLUDED_)

#define AFX_GREETING_H__34246B3E_5382_401E_9265_7B6DFEFA39AF__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class Greeting
{
public:
void PrintMessage();
Greeting();
virtual ~Greeting();

private:
char* message;
};

#endif // !defined(AFX_GREETING_H__34246B3E_5382_401E_9265_7B6DFEFA39AF__INCLUDED_)

Select Greeting.cpp file and edit it. Enter a line

message="Hello World!\n";

inside the Greeting class constructor

Greeting::Greeting()
{
message = "Hello World\n";
}

Enter line of code inside the PrintMessage function

printf(message);

void Greeting::PrintMessage()
{
   printf(message);
}

Enter a line #include < stdio.h > under existing #include lines

#include "stdafx.h"
#include "Greeting.h"
#include <stdio.h>

Your Greeting.cpp file should be like this:

Greeting.cpp: implementation of the Greeting class.

//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Greeting.h"
#include <stdio.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Greeting::Greeting()
{
    message = "Hello World\n";
}

Greeting::~Greeting()
{
}

void Greeting::PrintMessage()
{
   printf(message);
}

Select HelloWorld.cpp file and enter the following two line of code in main function.

Greeting gr;
gr.PrintMessage();

Now your HelloWorld.cpp file must be like this:

// HelloWorld.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include "Greeting.h"

int main(int argc, char* argv[])
{
Greeting gr;
gr.PrintMessage();
return 0;
}

Do not edit stdafx.h file.

From main menu select File, Save Workspace.

Select Build, Build HelloWorld.exe

C++ tutorial: Build Executable

--------Configuration: HelloWorld - Win32 Debug--------------------

HelloWorld.exe - 0 error(s), 0 warning(s)

Number of Errors displays at the bottom of the screen. If you get errors, please check your spelling.

Select Build, Execute HelloWorld.exe from main menu

C++ tutorial: Execute Application

"Hello World!" message displays on Command prompt window.

Congratulation! You just created your first application in C++. How it works?

In spite of simplicity, our application is an example of the Object Oriented Program. The Object Oriented Programming based on a concept that all things of the Universe might be represented as objects with certain properties and methods. For example, your car is an object. It has many different properties: name, size, color, price etc. It has functions or methods: it can move, play music, produce light etc. Your cat also is an object. It has properties: fur, size, color, name etc. It has methods: it jumps, it catches mice. In computer programming we use classes to describe objects. Cat class would describe all cats that have common properties and methods. Your particular cat is an instance of Cat class. It has properties and methods common with other cats and some properties and methods that are unique. In our application we use Greeting class. This class has one property: message and one method PrintMessage().

Execution of any C++ application starts from main procedure. In main procedure we declare an instance of Greeting class

Greeting gr;

Then we call its method.

gr.PrintMessage()

At that moment Greeting class constructor is executed and an instance of Greeting class is created. Then PrintMessage() method is executed and message displays on the screen.

Inside the class constructor we have the following code

char* message ="Hello World";

char* is a pointer to array of characters. It points to the address of the first character of the array.

Hello  World

The function printf(message); read array of characters and print it on the screen.

Continue

Password Keeper VB Application


Please rate the tutorial

1 2 3 4 5 6 7 8 9 10



Learn SQL Programming By Examples [Kindle Edition]2.99

Learn PHP Programming by Examples [Kindle Edition] $2.99

Learn Visual Basic 6.0 [Kindle Edition] $1.99

How to Build Your Own Web Site from Scratch [Kindle Edition] $1.49

New-trip.com website source code

Comments
 
Register to add comments ( 1000 char ) for visualc.php.