Beginning Visual C++ 6.0

ravi varma

Club Cricketer
Joined
Mar 17, 2008
Location
Usa/newyork
Online Cricket Games Owned
Introduction

This tutorial is going to cover the basics of Visual C++. I will be covering alot of the basics, so this should be a good start to anyone wanting to learn C. I could go into more details on all the functions, classes, objects ect. but that is another article for another time.

How it works...

Visual C++ 6.0 is designed to help you build your C++ programs and has a debugger and compiler and everything all in one, so you don't have to bother with any of that. The source code you write will be saved in a file with a .cpp extension (the cpp stands for C plus plus). The compiler will translate the higher-level instructions you wrote into machine language ( 1's and 0's). This is what the computer does understand. So when you compile the program, the compiler will also create a file with a .obj extension (obj for object). And finally, once the compiler creates the object file, the linker is then executed. The linker will take the object file and combine additional machine code necessary for it to run correctly, and an executable file is then created, which has an extension of .exe (exe for executable). If all is well, you can run the .exe file over and over without need of anymore translating and such. This is your final product.

Algorithms, Flowcharts and IPO Charts

I have worked with numerous programming languages. Each time you're learning the code; programs, flowcharts, or algorithms will be mentioned to help you in the design process. Generally, I would ignore these because it was easier for me to write the program, and if it was required, I would go back and whip up a flowchart or algorithm. However, when I began learning Visual C++, It does help if you're at a beginning level to use these to your advantage.

Flowcharts and algorithms are not the only way to plan out your program. Programmers also use what are called IPO charts to organize and summarize the results of their problem analysis. IPO is for Input, Process and Output. This is a simple table that has 3 columns. The first column is Input. In this column, place what you will need as input for the program. What data will be required as user input, like hours worked, or rate of pay. Then, the second column is Processing and processing items. This is where you will make up an algorithm for how your program will work. The processing items will be like counters, ect. Finally, the third column is for Output. What will your results be? What do you need to show to the user. This data will be here.

This is all to help you out, and as you begin to disect various programming problems you will get better each time and eventually, you may begin to shy away from IPO charts, and that of the like. But even further on, you may find them quite useful to break down really large programs that you need to work on.

Declaring your variables

If you are experienced in other programming laugages like visual basic or javascript, you will find out that they have made you life quite easy when it comes to coding. The number one difference you will find opposed to other various langauges is that you are required to declare each variable and its type before it is used. In programming before, you could just say:

$num1 = 2; $num2 = 8; $ans = $num1 + $num2;

This is no longer the case. You will get errors (in the linker at the bottom of Visual C++) like, "Unidentified identifier" or "undeclared variable" --something like that. The good thing is, that it will give you an idea of where the error is. Try not to pay too much attention to the exact line, because that may not always be the case. Anyways, If you wanted to do the calculation above in Visual C++, it would look like this:

int num1 = 2; int num2 = 8; int ans = 0; ans = num1 + num 2;

Don't worry about your variable ans being = to zero at first. This is just the initial value that needs to be set.

DATA TYPES

Int stands for integer. And their are other types of integers (short, long, and int).

There are also Floating-point numbers. These are numbers that contain a decimal. And there are other types of float numbers (float, double and long double). To declare a floating point number, it would look like this:

float num1 = 0.00;

quite similar to int, just remember it has a decimal, so when you declare it use a decimal.

char is another type. This is short for character. A char is one character enclosed in single quotation marks. Some programmers pronounce it as "care", others say "char" like in charcoal- at any rate, you would declare a char like this:

char something = 'a';

String is a data type you will use. This is zero or more characters enclosed in double quotation marks. When you use a string, this is what the code should look like:

string str1 = "Have a nice day.";

Finally their are Boolean values. If you do not know, Boolean is either True or False. Instead of using the full word Boolean like in Visual Basic, you will use bool. Take a look below:

bool val1 = True; bool val2 = False;


Declaring Variables

You want to make it a habit to declare your variables correctly, and not to mix them back and forth, this will cause tedious errors in your programs and can produce weird results because they are stored in a special memory location depending on the type. It can get quite technical. I'll explain a bit.

The computer stores numeric data in binary code, and character data in ASCII codes. 0 (zero) in AScII = 48; 0 (zero) in binary = 00110000. C++ will store it according to the data type -- so say you have the number 9. Well, if you assign that the type int: its memory location will read as the binary number 1001 (one 1 + one 8). If the data type is char: its memory location will will be stored as a character using ASCII code 57, which is represented in internal memory as 00111001 (one 1 + one 8 + one 16 + one 32). Catching on?

I will show you in a code sample, and you can later on execute the code to see the results for yourself.

int var1 = 01000001; char var2 = 01000001; cout << var1 << endl; cout << var2 << endl;

the results will read for var1: 65, and for var2: A. Even though they equal the same thing, the results are different based on the data type, because 01000001 is interpreted for a character as the ASCII code 65, which is equivalent to the letter, A. However, when you declare the type as int, 01000001 is interpreted as the binary code for decimal number 65.


Basic Structure

Your basic layout for C++ programs can be different that what you are normally use to. The first line of your program will identify the header file you will be using. The header file I will be using is . There are many different ones that can be used. Each header file has its own syntax for performing various actions.

The beginning of each of the code samples and programs I use will look like this:

#include <iostream> //contains input and output operations #include <string> //contains instructions to reserve and access string memory locations. using namespace std; //Tells the compiler that the definitions of keywords can be found //in a namespace named std (std short for standard). int main() { //declare your variables here at the beginning string str1 = ""; //peform your calculations str1 = "hello world"; //show your results cout << "hello world" << endl; return 0; } //end of main function

*Note, at the end of the main function you see "return 0;" - Once the function has completed everything, this will return to the OS the number 0 to indicate the successful ending.


Creating a Console Application

Again, this tutorial is for Visual C++ and for this part, it would be quite helpful if you install it on your machine so that you can follow along. But If you already have a copy, then you can start off by opening Visual C++.

1. Click file --> New, and a new box will open.
2. Click on the Projects tab and select the option that reads, "Win32 Console Application" from the list of project types.
3. Now click on the Create new workspace option.
4. You can name your project in Project name textbox.
5. After you have named your project, click OK to close the dialog box.
6. A new dialog box will open that reads, "Win32 Console Appication - Step 1 of 1". You want to select the option that reads, "An empty project".
7. Click finnish and the New Project Information box will appear that shows you the details, click Ok to continue.


From here, you have now created a project and a workspace. The workspace will hold several projects, and your projects hold the source code files, ect. You have not added a source file to your project, so to do that, follow the steps below:

1. Click Project from the top menu bar and navigate to --> Add to Project, and click New.
2. Now select the Files tab from the new dialog box, and select C++ Source File from the list below.
3. Now just name it using the text box under, "File name" --Click Ok and you now have a source code file to begin you work!


Be careful not to open several different source code files under the same project and attempt to compile them. Make sure that each project has only the files it needs, and start new workspace for different work. I say this because if you're working on several source files, and try to compile and you're getting an error, but cant find it -- the compiler is compiling all the source code files in the workspace, so this can create conflicting problems that are un-necessary.

cin and cout

cin and cout are two streams you use for input and outputting data. You will combine these streams with an insertion operator (<<) and extraction operator (>>). The insertion operator goes with cout stream, and the extraction operator goes with cin. "<<" inserts information into the cout stream to display on the screen, and ">>" takes data and stores into cin from the keyboard. Take a look below how they work:

string myBeverage = "Mountain Dew"; cout << "I drink " << myBeverage << endl;

This would display to the screen: "I drink Mountain Dew".

cin:
int age = 0; cout << "how old are you? " << endl; cin >> age;


This will print on the screen, "How old are you?" and when you enter a number, "cin << age" stores the number entered into the age variable.

getline

cin gets information from the user. But what if you wish to get a string from the user? To do this, you must use the getline(); function. The getline function has two arguments between its parenthesis. The first argument is for the source of the input, and the second is the variable to store the input received. heres the syntax: getline(cin, variable); just as you see. Glance below to see an example of cin using getline:

string name = ""; cout << "What is your name? " << endl; getline(cin, name);

The code sample will display, "What is your name? " And wait for you to enter your name. getline(); stores the string you enter into the variable "name". So, after this, if you were to add the code, cout << "Your name is:" << name << endl; It would show, "Your name is: <whatever you typed>.

endl

Notice the endl; at the end of the code samples above. This is very simple to remember, it just takes the cursor and moves it to the next line, what they call a hard break return. If you used two endl's, it would take the cursor down two lines after the statement. It is the same as using "
" in HTML.

Arithmetic Operators and Math Functions

Visual C++ uses alot of the normal arithmetic operators you might currently be use to.

* ( ) - Overrides normal presedence rules.
* * - for Multiplication.
* / - Used for Division.
* % - Modulus (unlike visual basic, where you would use "Mod").
* + - Addition, as you might guess.
* '-' - Negation or subtraction. Difference is that negation is unary, subtraction is binary. Unary requires one operand by the operator, binary requires two.


Notice that there is no "^" to represent exponents, like VB. This is because Visual C++ does not support that operator (like javascript). However, a function is provided to perform exponentiation. This is the pow(); function. In fact, there are many math functions that C++ does support, I will list a few:

*Note: These functions are defined in <cmath> ; so if you plan to use these in your source code, be sure to include it just as you included iostream.

* Square root - If you wish to find the square root of x, use: sqrt(x);.
* Sine - To find sine of x, use: sin(x);
* Cosine - To find cosine of x, use: cos(x);
* Tangent - To find the tangent of x, use tan(x);


Here is a complete code sample that uses a bit of what we have learned thus far:

#include <iostream> #include <cmath> #include <string> using namespace std; int main() { //our variables used int age = 0; string name = ""; int num1 = 0; int num2 = 0; float answer = 0.00; //calculations cout << "Enter your name: "; getline(cin, name); cout << "Enter your age: "; cin >> age; cout << "Enter any number: "; cin >> num1; cout << "Enter another number: "; cin >> num2; answer = num1 * num2; //display results cout << name << ", You are " << age << " years old." << endl; cout << num1 << " * " << num2 << " = " << answer << endl; return 0; } //end of main function

More From
C and Cpp Introduction to C and Cpp Beginning Visual C++ 6.0 Tutorial
 
Thanks, I found that interesting. I've not used C++ yet but will probably have to move away from the comfort of .net at some point...
 
Are you saying that C++ is behind .net? Kshitiz keeps trying to convince me of the opposite.
Found this quote from Linus Torvalds:

C++ is a horrible language. It’s made more horrible by the fact that a lot of substandard programmers use it, to the point where it’s much much easier to generate total and utter crap with it. Quite frankly, even if the choice of C were to do *nothing* but keep the C++ programmers out, that in itself would be a huge reason to use C.

In other words: the choice of C is the only sane choice.
 
Are you saying that C++ is behind .net? Kshitiz keeps trying to convince me of the opposite.
Found this quote from Linus Torvalds:
I think you will find that Linus Torvalds is generally pretty opinionated. I remember reading a huge tirade last summer about how all tabs should be 8 spaces wide or something like that and anyone who doesn't use that is a poor coder. He is also quite arrogant...

sohum added 14 Minutes and 49 Seconds later...

C++ is definitely not behind .NET. Most of the languages are implemented in C/C++. It may be harder but much more powerful. But it depends on your needs, if you ever need to learn C++. These days frameworks have made it easy to program almost anything. Unless you need to do low-level stuff like writing drivers or have concerns about speed, .NET will get the job done. The advantage of knowing C++ is that you get an idea of how these frameworks work internally and it makes it pretty easy to pick up a new language.
I don't think it's possible to say one is ahead or behind the other one. Each language has its pros and cons. C++ is currently requisite knowledge if you want to develop any scale of industrial application since you can get away without layers of abstraction of OOP.

However, with MS moving quickly towards .NET (the VS2010 IDE is going to be in C#/WPF) I think we'll see a stronger push towards managed code over the next decade.
 
Are you saying that C++ is behind .net? Kshitiz keeps trying to convince me of the opposite.
Found this quote from Linus Torvalds:
This is the guy who programmed the Linux kernel, isn't he? I'd think so him saying that, considering that Linux is programmed in C...

Fact is, for any type of application requiring processing power, you have to use something like C++ or suffer performance degradations - which generally aren't noticed in small applications.

C++ gives you the feel of doing everything your self - although this might be considered a burden sometimes, it is actually good.

From what I see, Frameworks are seldom more than 1000's of defined classes, making the job easier for the programmer. However you can see, the more you leave in the hand of the frameworks the less control you have on the program.

Another thing is, that C/C++ are multi platform. It is not a big deal nowadays, but with Linux venturing ahead, and who knows maybe more of them, portable code will always be favoured, whereas .NET is only MS specific.

Its a bit difficult to explain, but I've changed quite a lot [thanks to AbBh] in my views regarding this.

I'm not saying .NET is poor or anything, its great for making small, quick applications, but for anything bigger you need to move on to C/C++. Java, from what I've heard, is nothing but C++ with pre - defined classes.
 
NO... matter what u r doing or not. My elder bro says that there is a now a new programming
had been come C# it is just same as C++ so no worry about it.
 
From what I've heard c# is very similar to .net in terms of the libraries and frameworks so purist programmers will probably continue to use C++.
 
I'm not saying .NET is poor or anything, its great for making small, quick applications, but for anything bigger you need to move on to C/C++.
Like I said, Microsoft is beginning to phase out statements like this. Visual Studio, which is quite a large, robust application, is being driven by the .NET framework in its next release. The needs for C/C++ will soon become very niche markets with Moore's Law really taking effect for data storage.

Java, from what I've heard, is nothing but C++ with pre - defined classes.
Java compiles for the JRE whereas C/C++ compiles into the operating system specific native code (assuming you have the correct compiler, of course). If you're comparing Java to C++ otherwise, there is very little similar with them. Java is much more like C# than C/unmanaged C++ with garbage collection, OOP, and the like.

sohum added 4 Minutes and 42 Seconds later...

From what I've heard c# is very similar to .net in terms of the libraries and frameworks so purist programmers will probably continue to use C++.
C# IS a .NET language. It compiles into CLR. .NET languages range from C# to VB to F# (a functional programming language). They even used to have their own version of Java (J#) and they also have a version of C++ that is CLR (C++ CLI).

Imo, you really can't afford to be a "purist" if you're in software. Technology is one of the fastest growing industries in the world and to keep ahead of the competition you're going to have to pick up skills along the way. For example, scripting purists grew up on Perl. Perl still has its usages now but with the web we've seen a complete transformation into PHP, Python and Ruby.

Everyone is going to have their preferences for a language. But if you continue ignoring .NET for the next few years, when the transformation gradually happens, you'll be left behind...
 
To add... there is a currently active open-source project which is working on making .NET platform-compliant. It is called "Mono". It basically allows you to write C#/VB.NET and compile it to a Linux/OS-X/Windows executable.
 
The needs for C/C++ will soon become very niche markets with Moore's Law really taking effect for data storage.
Everyone is going to have their preferences for a language. But if you continue ignoring .NET for the next few years, when the transformation gradually happens, you'll be left behind...
That is only the case for application programmers. Operating systems, anti-viruses, compilers and most likely games/engines will still have to be largely written in C/C++ and ASM.
 
That is only the case for application programmers. Operating systems, anti-viruses, compilers and most likely games/engines will still have to be largely written in C/C++ and ASM.
Hence the phrase niche markets. Anything running at kernel mode obviously can't depend on a run-time engine. I'd say of that list, compilers and games/engines are going to transform. The guy who heads the Mono project started off by writing a C# compiler in C#.

Keep in mind that C/C++ have been there since the 70s. They are approaching their 40th anniversary in the next couple of years. C#, in contrast, isn't even 10 years old yet. When C/C++ were first introduced, I'm sure the old-school assembly programmers rubbished it.
 
Shouldn't these be in Education forum?
 

Users who are viewing this thread

Top