- Home
- Registration
- Database Design
- Password Keeper
- MySQL and Excel
- Learn MySQL
- Learn PHP
- Learn Visual Basic
- Learn Java
- Web Site Tips
- Miscellaneous
- C ++ Code Examples
- Start C++
- C ++ Continue
- C ++ Pointer
- Variable Scope
- Linked List
- Bubble Sort
- Two Bubble Sort
- Insertion Sort
- Selection Sort
- Two Selection Sort
- Merge Bin Sort
- Advertise here
- Site map
Start Programming in Visual C++ 6.0 - C++ code example.
By Sergey Skudaev
![]()
![]()
![]()
![]()
![]()
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
Select A Simple Win32 Application and click OK
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.
The New Class window displays. Enter class name Greeting (it may be anything)
Click OK button. Greeting class will be created. Select ClassView tab.
Right click on Greeting class and select 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
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.
System created Greeting.h, Greeting.cpp files.
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
--------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
"Hello World!" message displays on Command prompt window.
Congratulation! You just created your first application in C++. How it works?
Request free online bachelor degree information today!
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.
| H | e | l | l | o | W | o | r | l | d |
The function printf(message); read array of characters and print it on the screen.
Password Keeper VB Application
Did you find information useful? Send to your friend a link to this page
Please rate the tutorial
| Comments |
|---|
Web programming Tips