0% found this document useful (0 votes)
37 views19 pages

C++ Notes

C++ notes

Uploaded by

Insomnic Bebe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views19 pages

C++ Notes

C++ notes

Uploaded by

Insomnic Bebe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Aditi computer education programming c++ notes

1. Introduction to C++

History and Features of C++


C++ is a programming language created by Bjarne Stroustrup in 1979. It was designed as an extension to the C
programming language to support Object-Oriented Programming (OOP). C++ is widely used for system software,
game development, and applications that require high performance.

Main features of C++:

 Object-Oriented: It allows programmers to use concepts like classes and objects.


 Low-Level Manipulation: C++ can work closely with hardware, which makes it fast.
 Rich Library : It has a large collection of built-in functions.
 Portable : Code written in C++ can run on different computers with little or no modification.

Structure of a C++ Program


A C++ program generally has the following structure:

#include<iostream.h> // Includes the standard input-output library


#include<conio.h>
Void main () { // the main function is where the program starts
running Cout << "Hello, World!" ; // Outputs text to the screen
}

For example:

#include<iostream .h>
#include<conio.h>
void main() {
cout << "Hello, Students!";
}

This program prints "Hello, Students!" to the screen


Aditi computer education programming c++ notes
Key components:
 #include<iostream> : This line includes the input/output functions like `cout`.
 void main()`: This is the main function where the program execution starts.
 cout`: This is used to print output to the screen.
 "Hello, World!": This is the message that will be displayed on the screen.

Writing and Compiling a Simple C++ Program


In a simple C++ compiler, follow these steps:

1. Open the C++ editor and type the program.

2. Press the Compile button or use the shortcut `Alt + F9` to check for errors.

3. Press the Run button or use the shortcut `Ctrl + F9` to run the program and see the output.

Comments in C++
Comments help make the code easy to understand for humans, but they are ignored by the
compiler.

 Single-line comment: Use `//` for a comment that spans one line.

// This is a single-line comment

 Multi-line comment: Use `/* */` for comments that span multiple lines.

/* This is a
Multi-line comment */
Example:

#include<iostream.h>
#include<conio.h>
void main() {
}
// This program prints a greeting message

cout << "Hello, Class!"; } // Display message

wchar_t wideChar = L'Ω'; // Greek


letter Omega
cout << "Wide character: " <<
wideChar;
getch();
}
Aditi computer education programming c++ notes

2. Data Types and Variables in C++


Basic Data Types in C++
1. Int (Integer): Stores whole numbers (both positive and negative).

(Memory Size: 2 or 4 bytes depending on the system.)

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int num = 10;
cout << "Integer value: " << num;
getch();}

2. float (Floating Point): Stores decimal numbers (single precision). Memory Size:4 bytes.

#include<iostream.h>
#include<conio.h>
void main() {
clrscr();
float decimal Value = 3.14;
cout << "Float value: " << decimalValue;
getch(); }
3. double (Double Precision Floating Point)

Stores larger decimal numbers (double precision).

**Memory Size:** 8 bytes.

#include<iostream.h>
#include<conio.h>

void main() {
clrscr();
double largeDecimal = 9.87654321;
cout << "Double value: " << largeDecimal;
getch();
}
```
Aditi computer education programming c++ notes
4. char (Character)

Stores a single character or ASCII value.

**Memory Size:** 1 byte.

#include<iostream.h>

#include<conio.h>

void main() {clrscr();

char letter = 'A';

cout << "Character: " << letter; getch();}

`5. bool (Boolean)

Stores `true` or `false`.


Memory Size: 1 byte.

#include<iostream.h>
#include<conio.h>
void main() {
clrscr();
bool isTrue = true;
cout << "Boolean value: " << isTrue;
getch(); }
6. void

Represents no value. This data type is typically used in functions to indicate that they
don't return a value.

Memory Size: No memory is allocated for `void`.


Aditi computer education programming c++ notes

Practice Turbo C++ Code (Combining Different Data Types)Here’s a program


that demonstrates the use of different data types together in a single code
example:

#include<iostream.h>
#include<conio.h>

void main() {
clrscr();

int age = 21;


float height = 5.9;
double weight = 72.567;
char grade = 'A';
bool isPassed = true;

cout << "Integer (Age): " << age << endl;


cout << "Float (Height): " << height << endl;
cout << "Double (Weight): " << weight << endl;
cout << "Character (Grade): " << grade << endl;
cout << "Boolean (Passed): " << isPassed << endl;

getch();
}
`

Explanation:

- int age = 21; — Stores a whole number representing age.

- float height = 5.9; — Stores a floating-point number for height.

- double weight = 72.567; — Stores a more precise floating-point number for weight.

- char grade = 'A'; — Stores a single character for the grade.

- bool isPassed = true; — Stores a boolean value representing the pass status.

.
Aditi computer education programming c++ notes

Syntax for Declaring a Variable in C++


In C++, a **variable** is a named location in memory used to store data. Each variable
has a data type that determines what kind of data it can hold (e.g., integers, floating-point
numbers, characters).

data_type variable_name = value;

data_type: The type of the variable (e.g., `int`, `float`, `char`).

variable_name: The name you give to the variable.

value (optional): You can initialize the variable with a value when you declare it.

Example Types of Variables in C++

1. Integer (int): Stores whole numbers.

2. Float (float): Stores decimal numbers.

3. Character (char): Stores single characters.

4. Double (double): Stores large decimal numbers with more precision.


Aditi computer education programming c++ notes

Turbo C++ Code Example for Variables

Here is an example program that demonstrates how to declare and use different
types of variables in Turbo C++:

#include<iostream.h> // For input/output operations


#include<conio.h> // For Turbo C++'s console functions

void main() {
clrscr(); // Clear the screen (specific to Turbo C++)

// Declaring variables
int age = 20; // Integer variable
float height = 5.9; // Float variable
char grade = 'A'; // Character variable
double salary = 50000.50; // Double variable

// Output the values of the variables


cout << "Age: " << age << endl;
cout << "Height: " << height << endl;
cout << "Grade: " << grade << endl;
cout << "Salary: " << salary << endl;

getch(); // Wait for a key press (specific to Turbo C++)


}
Aditi computer education programming c++ notes

Keywords and Identifiers in C++

Keywords

-Keywords are reserved words in C++ that have a specific meaning and cannot be used for
other purposes like variable names or function names. They are part of the language
syntax.

- Examples of keywords: `int`, `float`, `if`, `else`, `for`, `while`, `return`, `class`, `public`,
etc.

Identifiers

- Identifiers are names given to elements like variables, functions, arrays, etc., in a
program by the user. Identifiers can be any combination of letters and digits, but they
cannot start with a digit. They also cannot be one of the reserved keywords.
Rules for Identifiers:

1. The first character must be a letter or an underscore (`_`).

2. After the first character, it can be letters, digits, or underscores.

3. Identifiers are case-sensitive (`myVariable` and `MyVariable` are different).

4. You cannot use keywords as identifier


Aditi computer education programming c++ notes

#include <iostream.h>
#include <conio.h>

void main() {
// Keywords in C++: int, float, return, etc.
int num1; // 'int' is a keyword
float num2; // 'float' is a keyword

// Identifiers: num1, num2, result, etc.


int result; // 'result' is an identifier

clrscr(); // Clears the screen (Turbo C++ specific)

cout << "Enter the first number: ";


cin >> num1;

cout << "Enter the second number: ";


cin >> num2;

// Performing addition and storing result


result = num1 + num2;

// Output the result


cout << "The sum of " << num1 << " and " << num2 << " is: " << result <<
endl;

getch(); // Waits for user input (Turbo C++ specific)


}

Explanation:
- Keywords Used:
- `int`, `float`, `return` are keywords used to define data types and functions.
- Identifiers Used:
- `num1`, `num2`, `result` are user-defined identifiers representing variable names.
In this code:
- `int` and `float` are **keywords** representing integer and floating-point types.
- `num1`, `num2`, `result` are **identifiers** used to represent variables.
Aditi computer education programming c++ notes

Control Structures in C++


Control structures allow programmers to control the flow of execution in a C++ program.
They are crucial for making decisions, looping through data, and managing program flow.
There are three main types of control structures in C++:
1. Sequential Control Structure: Executes statements sequentially, one after the other.
2. Selection (Decision) Control Structure: Used for making decisions.
3. Iteration (Looping) Control Structure: Used for repeating a block of code.

Here’s a breakdown with Turbo C++ code examples:

1. Selection Control Structure

a. if Statement
The `if` statement allows a program to execute a block of code based on a condition.

#include<iostream.h>
#include<conio.h>

void main() {
clrscr(); // Clear screen (Turbo C++ specific)
int num;
cout << "Enter a number: ";
cin >> num;

if (num > 0) {
cout << "The number is positive.";
}

getch(); // Wait for user input (Turbo C++ specific)


}
```
Aditi computer education programming c++ notes

b. if-else Statement
The `if-else` statement provides two paths of execution depending on whether the
condition is true or false.

#include<iostream.h>
#include<conio.h>

void main() {
clrscr();
int num;
cout << "Enter a number: ";
cin >> num;

if (num > 0) {
cout << "The number is positive.";
} else {
cout << "The number is non-positive.";
}

getch();
}
Aditi computer education programming c++ notes

c. switch Statement
The `switch` statement allows multiple possible execution paths based on the value of a
variable.

#include<iostream.h>
#include<conio.h>

void main() {
clrscr();
int choice;
cout << "Enter your choice (1-3): ";
cin >> choice;

switch (choice) {
case 1:
cout << "You chose option 1.";
break;
case 2:
cout << "You chose option 2.";
break;
case 3:
cout << "You chose option 3.";
break;
default:
cout << "Invalid choice!";
}

getch();
}
```
Aditi computer education programming c++ notes

2. Iteration Control Structure


a. while Loop
The `while` loop continues executing a block of code as long as the condition is true.

#include<iostream.h>
#include<conio.h>

void main() {
clrscr();
int i = 1;

while (i <= 5) {
cout << i << " ";
i++;
}

getch();
}

b. do-while Loop
The `do-while` loop is similar to the `while` loop, but it guarantees that the code inside the
loop will run at least once.

#include<iostream.h>
#include<conio.h>
void main() {
clrscr();
int i = 1;
do {
cout << i << " ";
i++; } while (i <= 5); getch();}
Aditi computer education programming c++ notes
```

c. for Loop
The `for` loop is used when the number of iterations is known beforehand .

#include<iostream.h>
#include<conio.h>

void main() {
clrscr();

for (int i = 1; i <= 5; i++) {


cout << i << " ";
} getch();
}
```

3. Jump Control Structures

a. break Statement
The `break` statement is used to exit a loop or a switch statement prematurely.

#include<iostream.h>
#include<conio.h>

void main() {
clrscr();

for (int i = 1; i <= 5; i++) {


if (i == 3) {
break;
}
cout << i << " ";
}
getch();}
```
Aditi computer education programming c++ notes

b. continue Statement
The `continue` statement skips the current iteration of a loop and moves to the next
iteration.

#include<iostream.h>
#include<conio.h>

void main() {
clrscr();

for (int i = 1; i <= 5; i++) {


if (i == 3) {
continue;
}
cout << i << " ";
}

getch();
}
```

These examples cover the essential control structures in C++ using Turbo C++. You can
use them to manage decision-making and loops in your programs.
Aditi computer education programming c++ notes

1. Primitive Data Types

Primitive data types are the basic building blocks for data in C++. Here are the common
ones:

int: Used to store whole numbers (e.g., -3, 0, 7).

int age = 25; // Here, age is an integer variable that stores the value 25.

float: Used to store decimal numbers (e.g., 3.14, -0.001).

float price = 19.99; // price is a float variable storing a decimal value.

-double: Similar to float but can store larger decimal numbers with more precision.

double distance = 12345.6789; // distance is a double variable for more precise decimal.

char: Used to store a single character (e.g., 'a', 'Z', '1').


char grade = 'A'; // grade is a char variable storing the character 'A'.

bool: Used to store boolean values, which can be either true or false.

bool isStudent = true; // isStudent is a bool variable that stores the value true.
Aditi computer education programming c++ notes

2. Declaring and Initializing Variables

Declaring : a variable means telling the program what type of data it will hold.

Initializing: means giving it a starting value.

`
int height; // Declaration: height can store an integer.
height = 180; // Initialization: height is now set to 180.

float weight = 75.5; // Declaration and initialization in one line.

3. Constants and Literals

Constants: are fixed values that do not change during the program execution. You can use
the `const` keyword to declare them.

const int MAX_SCORE = 100; // MAX_SCORE is a constant that will always be 100.

**Literals: are the actual values you assign to variables (like 25 in `int age =
25;`).
Aditi computer education programming c++ notes

4. Type Modifiers

Type modifiers change the size and sign of the data types. Here are some common
modifiers:

short: Used for small integers.

short smallNumber = 30000; // smallNumber is a short integer.


`

long: Used for larger integers.

long largeNumber = 1234567890; // largeNumber is a long integer.


```

signed: Indicates that the variable can hold both positive and negative values.
It’s the default for int, but can be explicitly stated.

signed int temperature = -10; // temperature can be negative.


```

unsigned: Indicates that the variable can only hold non-negative values (0 and
positive).

unsigned int positiveCount = 5; // positiveCount cannot be negative .


```
Aditi computer education programming c++ notes
Summary Example

Here's a simple program that combines these concepts:

#include <iostream.h>
#include <conio.h>
int main() {
// Primitive data types
int age = 25; // An integer variable
float height = 5.9; // A float variable
double salary = 50000.50; // A double variable
char grade = 'A'; // A char variable
bool isEmployed = true; // A bool variable

// Constants
const int MAX_ATTEMPTS = 3; // A constant variable

// Outputting the values


cout << "Age: " << age << endl;
cout << "Height: " << height << endl;
cout << "Salary: " << salary << endl;
cout << "Grade: " << grade << endl;
cout << "Is Employed: " << isEmployed << endl;
cout << "Max Attempts: " << MAX_ATTEMPTS << endl;
getch();
return 0;
}

This program declares various types of variables, initializes them, and prints their values
to the console. Each part demonstrates how to use data types and variables in a
straightforward way!

You might also like