[go: up one dir, main page]

100% found this document useful (1 vote)
19 views4 pages

What Is C++

C++ is a cross-platform, object-oriented programming language developed as an extension of C, allowing for high-performance applications and code reuse. It supports classes and objects, distinguishing it from C, and features a syntax that includes input/output operations, variable declarations, and user input handling. The document also provides examples of C++ syntax, variable types, and basic operations such as printing text and numbers.

Uploaded by

xubairch596
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
19 views4 pages

What Is C++

C++ is a cross-platform, object-oriented programming language developed as an extension of C, allowing for high-performance applications and code reuse. It supports classes and objects, distinguishing it from C, and features a syntax that includes input/output operations, variable declarations, and user input handling. The document also provides examples of C++ syntax, variable types, and basic operations such as printing text and numbers.

Uploaded by

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

What is C++?

C++ is a cross-platform language that can be used to create high-


performance applications.

C++ was developed by Bjarne Stroustrup, as an extension to the C language.

C++ can be found in today's operating systems, Graphical User Interfaces,


and embedded systems.

C++ is an object-oriented programming language which gives a clear


structure to programs and allows code to be reused, lowering development
costs.

Difference between C and C++


C++ was developed as an extension of C, and both languages have almost
the same syntax.

The main difference between C and C++ is that C++ supports classes and
objects, while C does not.

C++ Syntax
Example
#include <iostream>
using namespace std;

int main() {
cout << "Hello World!";
return 0;
}

Line 1: #include <iostream> is a header file library that lets us work with
input and output objects, such as cout.

Line 2: using namespace std means that we can use names for objects and
variables from the standard library.

Line 3: A blank line. C++ ignores white space. But we use it to make the
code more readable.

Line 4: Another thing that always appear in a C++ program is int main().
This is called a function. Any code inside its curly brackets {} will be
executed.

Line 5: cout (pronounced "see-out") is an object used together with


the insertion operator (<<) to output/print text.

Line 6: return 0; ends the main function.


C++ Output (Print Text)
The cout object, together with the << operator, is used to output values and
print text.

Just remember to surround the text with double quotes ( ""):

C++ Print Numbers


You can also use cout() to print numbers.

However, unlike text, we don't put numbers inside double quotes:

Example
#include <iostream>
using namespace std;

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

You can also perform mathematical calculations:

Example
cout << 3 + 3;

Example
cout << 2 * 5;

New Lines
To insert a new line in your output, you can use the \n character:

cout << "Hello World! \n";


cout << "I am learning C++";

You can also use another << operator and place the \n character after the
text, like this:

cout << "Hello World!" << "\n";


cout << "I am learning C++";

Tip: Two \n characters after each other will create a blank line:
C++ Variables
Variables are containers for storing data values.

In C++, there are different types of variables (defined with different


keywords), for example:

 int - stores integers (whole numbers), without decimals, such as 123


or -123
 double - stores floating point numbers, with decimals, such as 19.99 or
-19.99
 char - stores single characters, such as 'a' or 'B'. Char values are
surrounded by single quotes
 string - stores text, such as "Hello World". String values are
surrounded by double quotes
 bool - stores values with two states: true or false

Declaring (Creating) Variables


To create a variable, specify the type and assign it a value:

Syntax
type variableName = value;

Example
Create a variable called myNum of type int and assign it the value 15:

int myNum = 15;


cout << myNum;

Example
int myNum = 5; // Integer (whole number without
decimals)
double myFloatNum = 5.99; // Floating point number (with
decimals)
char myLetter = 'D'; // Character
string myText = "Hello"; // String (text)
bool myBoolean = true; // Boolean (true or false)

Declare Many Variables


To declare more than one variable of the same type, use a comma-
separated list:
Example
int x = 5, y = 6, z = 50;
cout << x + y + z;

One Value to Multiple Variables


You can also assign the same value to multiple variables in one line:

Example
int x, y, z;
x = y = z = 50;
cout << x + y + z;

C++ User Input


cin is a predefined variable that reads data from the keyboard with the
extraction operator (>>).

Example
int x;
cout << "Type a number: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x; // Display the input value

1. calculate area of rectangle.


2. Calculate area of square.
3. Calculate average of five numbers.
4. Calculate profit and loss .

You might also like