Systems Programming Lec1b - CPP VisualStudio, Study notes of Computers and Information technologies

Summary about Systems Programming, Introduction to C , Introduction to Visual Studio, Variables,Strings and character arrays, multi-way decision.

Typology: Study notes

2010/2011

Uploaded on 09/10/2011

aristocrat
aristocrat 🇬🇧

5

(5)

240 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
1
Systems Programming II
Introduction to C++
Introduction to Visual Studio
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Systems Programming Lec1b - CPP VisualStudio and more Study notes Computers and Information technologies in PDF only on Docsity!

11

Systems Programming II

Introduction to C++

Introduction to Visual Studio

22

C++ Variables

Type Name (example) Value (example) int iCount; 100 long lGameScore; 7856098045 float fFractionOfGamesWon; 0. double dAreaOfCircle; 7. char cCode; X bool bFlag; true Example assignments iCount = 100; bFlag = false; lGameScore += 1000; dAreaOfCircle = 3.1415 * dRadius * dRadius; Arrays (sequential, indexed collection of variables, very useful within a loop) Examples: int cScore[5]; //Array of 5 integers char cName[20]; //Array of 20 chars

44

C++ Variables

Strings and character arrays (2) To access the individual characters in a character array the index must be provided, e.g. to set the contents one character at a time: szWord[0] = ‘H’; szWord[1] = ‘E’; szWord[2] = ‘L’; szWord[3] = ‘L’; szWord[4] = ‘O’; szWord[5] = 0; // Note its not in single quotes To copy the first letter into a different character variable: char cFirstLetter; cFirstLetter = szWord[0];

55

C++ Variables

Strings and character arrays (3) Strings can be manipulated using a group of methods: strcpy(str1, str2); // Copy string str2 into string str strcpy(szWord,”HELLO”); // This automatically adds the ‘null terminator’ 0 strncpy () // Copy the first ‘n’ characters from one string into another strcat () // Join one string on the end of another etc.

77

C++ Switch // multi-way decision

switch (iMessageType) { case 1 : // Process for iMessageType = 1 ... break; case 2 : // Process for iMessageType = 2 ... break; … default : // Process for all other cases. }

88

C++ Iteration (for, do and while)

// Allow controlled iteration / repetition of section of code int iPlayerScore[10]; // Valid index values are 0 … 9 int iCount = 0; int iTotal = 0; for( iCount = 0; iCount <10; iCount++ ) // Loop repeats with iCount = 0 … iCount = 9 { iTotal += iPlayerScore[iCount]; // This loop adds the iPlayerScore values } int I; int iTotal = 0; for( i = 0; i<=100; i++ ) { iTotal += i; // This loop adds the numbers 0, 1, 2, … 99, 100 } Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

1010

C++ Structures

// A collection of variables within a defined framework // Can refer to the structure itself or its individual members, as appropriate Example struct structPlayerDetails // structPlayerDetails becomes a new type { char sName[20]; int iScore; float fProportionOfGamesWon; }; Usage structPlayerDetails player1; // Create a variable player1 of the new type structPlayerDetails player2; structPlayerDetails players[2]; // An array of two player structure items Access to members player1.iScore = 25; players[1]. fProportionOfGamesWon = 0.65;

1111

C++ Classes and Objects

A class is a type of object (A class does not exist in code, and object does) e.g. CString is a class that defines string objects An object is an instance of a class, i.e. a variable of that type e.g. CString csName; // csName is an object, of class CString There can be many objects of the same type in a the same program, e.g. CString csPlayer1Name; CString csPlayer2Name; Objects can be grouped together in arrays, e.g. CString csPlayerName[10]; // An array of 10 CString objects (numbered 0 –

Use example CString s1 = “John"; CString s2 = “27"; CString message = “Player: “ + s1 + “ Score: " + s2;

1313

C++ Visual Studio – Introduction 2

Help - Highlight an item in the code and press F

  • also has ‘search’ and ‘index’ support Run-time step through and debug F5 run in debug mode CNTL F5 run without debug support F9 insert breakpoint F10 step , (over method) F11 step, (into method) CNTL F11 step, (out of method)

1414

C++ File Types

Header file (.h) Defines a class (its methods, variables etc). Source file (.cpp) Contains the actual methods of the class. Must ‘include’ the header file that contains the definitions for the class #include

Need the ‘Scope-resolution ‘ operator :: to associate each method with a specific class, e.g. CFirst_CPP_ProjectDlg::WriteResult() Good practice: 1 class per file