Skip to content

Lecture 5: Visual Studio

Learning Outcomes

By the end of this lecture, students should be able to:

  • Understand data types and variables in C++.
  • Apply various operators and manage data processing effectively.
  • Build and execute a C++ project using Visual Studio.

Creating a Project in Visual Studio

  1. Start Visual Studio 2022 -> Choose Create a new project.
  2. Select Empty Project and give it a name (e.g., Project_NTCU).
  3. Choose a save location -> Click Create.
  4. In Solution Explorer, a new solution appears with the chosen name.
  5. Right-click on Source Files -> Add -> New Item -> C++ File (.cpp).
  6. Name it (e.g., first_program.cpp) -> Click Add.
  7. To run the program:
    • Press F5 for Start Debugging, or
    • Press Ctrl + F5 for Start Without Debugging.

Debugging Options

  • F5 (Start Debugging): Attaches the debugger.
  • Ctrl + F5 (Start Without Debugging): Runs without debugging; console remains open.

Tip: To auto-close the console after debugging, go to
Tools -> Options -> Debugging -> Automatically close console when debugging stops.

Resolving main() Conflicts

When multiple .cpp files have main():

  1. In Solution Explorer, right-click the file -> Properties.
  2. Go to Configuration Properties -> General.
  3. Set Excluded from Build = Yes for files that shouldn't be compiled.

Visual Studio Project Structure

FileDescription
first_program.cppSource code file containing main() and program logic.
Project_NTCU.slnSolution file that manages all projects within Visual Studio.
Project_NTCU.vcxprojProject file with compiler and linker settings (XML format).
Project_NTCU.vcxproj.filtersOrganizes files visually in the Solution Explorer.
Project_NTCU.vcxproj.userStores user-specific settings (e.g., debugging options).

Basic C++ Examples

cpp
#include <iostream>
using namespace std;

int main() {
  cout << 9 + 3;
  return 0;
}

Escape Sequences

EscapeDescription
\nNew line
\tHorizontal tab
\\Backslash
\"Double quote

Example:

cpp
cout << "Hello World!\nI am learning C++";

Comments in C++

Used to improve code readability or disable parts of code.

cpp
// Single-line comment

/* Multi-line
   comment */

Variables and Data Types

Variables are containers for storing data. Each variable has a type that defines the kind of data it can hold.

Basic Data Types

TypeSizeDescription
int2–4 bytesWhole numbers
char1–2 bytesSingle character (ASCII)
string24–32 bytesText sequences
float4 bytesDecimal numbers (6–7 digits precision)
double8 bytesDecimal numbers (up to 15 digits precision)
bool1 bytetrue or false

Examples:

cpp
int age = 20;
float price = 19.99;
char grade = 'A';
string name = "Othman";
bool isStudent = true;

Numeric and Character Examples

Integer Example:

cpp
int x = 20;
cout << "x = " << x;

Float Example:

cpp
float myNum = 10.25;
cout << myNum;

Double Example:

cpp
double myNum = 10.999999992355;
cout << myNum;

Scientific Notation:

cpp
float f1 = 27E3;  // 27000
double d1 = 15e4; // 150000

Character Example:

cpp
char myGrade = 'A';
cout << myGrade;

ASCII Example:

cpp
char d = 68, b = 66;
cout << d << b; // Outputs DB

Strings

Declaration:

cpp
string myText = "Hello, World!";

Concatenation:

cpp
string fullName = firstName + lastName;
string mobile = "010" + "99985809";

Using append() Method:

cpp
string fullName = firstName.append(lastName);

Combining Numbers and Strings:

cpp
string result = "My number is: " + to_string(565655656);

Input Example

cpp
int x;
cout << "Type your number: ";
cin >> x;
cout << "Sum = " << x + 5;

Operators

Arithmetic Operators

OperatorNameExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y
%Modulusx % y
++Increment++x
--Decrement--x

Assignment Operators

OperatorDescriptionEquivalent
=Assignx = 9
+=Add and assignx = x + 9
-=Subtract and assignx = x - 9
*=Multiply and assignx = x * 9
/=Divide and assignx = x / 9
%=Modulus and assignx = x % 9

Comparison Operators

OperatorMeaningExample
==Equal tox == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater or equalx >= y
<=Less or equalx <= y

Example:

cpp
int x = 7, y = 5;
cout << (x == y); // false (0)

Logical Operators

OperatorNameDescriptionExample
&&ANDTrue if both are truea < 8 && a < 12
||ORTrue if one is truea < 9 || a < 10
!NOTReverses result!(a < 7 && a < 15)

Example:

cpp
int a = 8;
cout << (a > 4 && a < 10); // true (1)

Operator Precedence

PriorityOperators
1()
2*, /, %
3+, -
4>, <, >=, <=
5==, !=
6&&
7||
8=

Execution Order:

  1. Parentheses ()
  2. Multiplication/Division/Modulus
  3. Addition/Subtraction
  4. Comparisons
  5. Logical operations

Summary

  • Set up and manage C++ projects in Visual Studio.
  • Use basic syntax, variables, and data types effectively.
  • Apply escape sequences, comments, and operators.
  • Understand operator precedence and logical expressions.