Week 1: Getting Started with C++

 

 

Overview

C++ joins two separate programming traditions (procedural language and object-oriented programming language) - It will be fun to learn.

Windows 95 (Folders, File, Work Space)

Unix

AS/400, MVS, etc

C++ Builder

C: Dennis Ritchie of Bell Laboratories in 1970

C++: Bjarne Stroustrup of Bell Labs in early 1980

The OOP approach to program design is to first design classes that accurately represent those things with which the program deals

Binding of data and methods into class definition

Inheritance, Polym, etc

Top-down / bottom-up vs OOD

ANSI C

Microsoft C++

IBM Visual Age C++

Borland C++

Java

 

 

Programming with C++

 

Stage 1 Stage 2 Stage 3 Stage 4 Stage 5
         
Source Code Compiler Object Code Linker Executable Code
      Startup Code  
      Library Code  

 

Source Code: <filename>.cpp

Different ways of Compliation and Linking

Running: MS-DOS prompt, Unix xterm, AS/400 call

 

 

C++ Initiation

 

#include <iostream.h>			// Preprocessor directive
int main(void)
{
	cout << "The Classic Hello World" << endl;
	return 0;
}

 

The main() function

 

// General Format
int main(void)			// Vs void main(void), int main(), etc
{
	statements
	return 0;
}

 

Comments

// All the text behind this line will be commented

/*
 * ANSC C Style Comment
 * All the text between will be commented
 */
/*
 * /* What about this ? */
 *
 */
/*
 * // How about this
 *
 */
// Be careful on some platform, how long of a line it will take in general

 

Preprocessor and header files

C++, like C, uses a preprocessor.

It is a program that processes a source file before the main compilation take place.

#include <iostream.h> caues the preprocessor to add the contents of iostream.h file to your program
(Adding or replacing text is very common)

Can add your own header files

 

Sample Output

 

The cout Object The Insertion Operator A String
cout << "Hello World";

 

String "Hello World" inserted into output stream.

 

 

Source-Code Formatting

 

#include <iostream.h>			// Preprocessor directive
int main(void)
{
	cout << "The Classic Hello World" << endl;
	return 0;
}

 

/*
 * What about the following
 */
#include <iostream.h>			// Preprocessor directive
int 
main(void){
	cout 
		<< "The Classic Hello World" << endl;
	return 0;}

 

Declaration Statement and Assignment Statement

 

#include <iostream.h>			// Preprocessor directive
int main(void)
{
	int myNumber;
	myNumber = 28;
	cout << "The Classic Hello World number is ";
	cout << myNumber;
	cout << "\n";
	return 0;
}

 

 

cin

cin >> myInput;

cout << "Input is " << myInput << "of Value" << endl;

 

 

Functions

Declaration with prototyping

void report (int);

Implementation in the source code file

void report (int value)
{
	cout << "Reporting a value of " << value << endl;
}

Calling the function

report (25);

 

 

Demo and Lab

This lab give a brief introduction on how to use the C++ compiler, standard header files, basic function, C++ statements and source code format.

week1.cpp

week1.out