Basics of c++programming, Study notes of Computer Science

C++ basics for beginners to learn from starts

Typology: Study notes

2022/2023

Uploaded on 10/22/2023

pakiza-malik
pakiza-malik 🇵🇰

1 document

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Basics of C++
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Basics of c++programming and more Study notes Computer Science in PDF only on Docsity!

Basics of C++

Data types

int myNum = 5; // Integer (whole number) float myFloatNum = 5.99; // Floating point number double myDoubleNum = 9.98; // Floating point number char myLetter = 'D'; // Character bool myBoolean = true; // Boolean string myText = "Hello"; // String

Constant

A constant is a named data item with a predefined value. You cannot change the value assigned to a predefined constant. The predefined constants are: NULL: An empty reference. Similar to an empty pointer. Note that this is not the same as a null string

. TRUE : Equivalent to the number 1. FALSE: Equivalent to the number 0.

Variable rules

The general rules for naming variables are: Names can contain letters, digits and underscores Names must begin with a letter or an underscore (_) Names are case sensitive (myVar and myvar are different variables) Names cannot contain whitespaces or special characters like !, #, %, etc. Reserved words (like C++ keywords, such as int) cannot be used as names

Unary expressions

Logical operators

As with comparison operators, you can also test for true (1) or false (0) values with logical operators. Logical operators are used to determine the logic between variables or values:

Assignment operator

Assignment operators are used to assign values to variables. In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x :  Example int x = 10; int z = 50; x += 5;

C++ Comments C++ Comments Comments can be used to explain C++ code, and to make it more readable. It can also be used to prevent execution when testing alternative code. Comments can be singled- lined or multi-lined. Single-line Comments C++ Multi-line Comments

C++ Multi-line Comments Multi-line comments start with /* and ends with /. Any text between / and / will be ignored by the compiler:  Example / The code below will print the words Hello World! to the screen, and it is amazing */ cout << "Hello World!";

Difference between Source code & object code  1. Source Code: Source code refers to high level code or assembly code which is generated by human/programmer. Source code is easy to read and modify. It is written by programmer by using any High Level Language or Intermediate language which is human- readable. Source code contains comments that programmer puts for better understanding.

Keywords Keywords(also known as reserved words ) have special meanings to the C++ compiler and are always written or typed in short(lower) cases. Keywords are words that the language uses for a special purpose, such as void , int , public , etc. It can’t be used for a variable name or function name or any other identifiers. The total count of reserved keywords is 95. Below is the table for some commonly used C++ keywords.

List of keywords

2:write a c program to find area and circumference of circle #include using namespace std; int main() {   (^) int rad;  (^) float PI = 3.14, area, ci;  (^) cout<<"\nEnter radius of circle: ";  (^) cin>>rad;  (^) area = PI *rad * rad;  (^) cout<<"\nArea of Circle :", area;  (^) ci = 2 *PI * rad;  (^) cout<<"\nCircumference of Circle : %f ", ci); }

output