0% found this document useful (0 votes)
2 views6 pages

Introduction to C++ Programming Basics

C++ is an object-oriented programming language developed by Bjarne Stroustrup in the early 1980s, combining features of C and Simula67. It is statically typed, compiled, and supports multiple programming paradigms, making it versatile for various applications. The structure of a C++ program includes sections for documentation, linking, definitions, and the main function, with input and output operations handled by cin and cout.

Uploaded by

suji anand
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)
2 views6 pages

Introduction to C++ Programming Basics

C++ is an object-oriented programming language developed by Bjarne Stroustrup in the early 1980s, combining features of C and Simula67. It is statically typed, compiled, and supports multiple programming paradigms, making it versatile for various applications. The structure of a C++ program includes sections for documentation, linking, definitions, and the main function, with input and output operations handled by cin and cout.

Uploaded by

suji anand
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

Introduction to C++

 C++ is an object-oriented programming language.


 It was developed by Bjarne Stroustrup at AT&T Bell Laboratories in the
early 1980s.
 Bjarne Stroustrup aimed to combine the best features of C and Simula67 to
create a more powerful language supporting object-oriented programming
while retaining C's power and elegance.
 C++ is an extension of the C language with the significant addition of the
class construct feature from Simula67.
 Initially, the language was called "C with classes."
 In 1983, the name was changed to C++, where "++" is the C increment
operator, suggesting that C++ is an augmented version of C.
 C++ is a superset of C.

Features of C++
1. Statically Typed
 In C++, variable types (e.g., int, float, char) must be declared before they
are used.
 Type checking is done at compile time, which helps catch errors early.
 Example:
int x = 10; // 'x' must always hold an integer.
2. Compiled
 C++ code is translated (compiled) into machine code by a compiler
 This makes the program run faster, as it's directly executed by the
computer's CPU.
3. General-purpose
 C++ can be used to build a wide range of applications: operating systems,
games, web browsers, embedded systems, etc.
 It is not limited to one specific domain.

4. Case-sensitive
 C++ treats identifiers with different cases as different entities.
 Example:
int number = 5;
int Number = 10;
number and Number are two separate variables.
5. Multi-paradigm
 C++ supports multiple programming styles:
 Procedural programming: Focuses on functions and procedures.
 Object-oriented programming: Uses classes and objects.
 Generic programming: Uses templates to write type-independent
code.
6. Middle-level Language
 C++ combines the features of both high-level languages (like Java or
Python) and low-level languages (like C or assembly).
 You can work close to the hardware (e.g., using pointers) but also use
abstractions like classes and templates.

7. Portable but Platform Dependent


 C++ compiled programs can run on different machines as long as they have
the same operating system and compatible hardware architecture.

8. Extensible
 You can add new features or functionalities to existing code easily.

9. Memory Allocation
 C++ allows you to allocate memory during runtime using operators like new
and delete. This provides flexibility in managing memory:
10. Pointers
• C++ supports pointers, which store memory addresses.
They enable powerful features like dynamic memory, arrays, and function
arguments by reference
.

Structure of a C++ Program

The program written in C++ language follows this basic structure.


Documentation section
Linking section
Definition section
Global declaration section
Class Definition
Data members
Member function definition
Main function section
Sub-program section (user defined functions)
Documentation Section (Optional)
 The documentation section contains information about the program such as
the author, date, and program title. This section should be written in the
form of comments.
 Single-line comments are written using //
 Multi-line comments are written using /* ... */.
 Comments are ignored by the compiler and are not converted into object
code.

Link Section (Mandatory)


 The link section includes header files and namespaces.
 For example, #include <iostream> is used to include the input/output stream
for cin and cout.
 The namespace is used to avoid name conflicts and defines the scope of
identifiers.

Definition Section (Macro Definition) (Optional)


 This section includes macro definitions using the #define directive.
 Macros are constants or expressions that are defined to be used throughout
the program.

Global Declaration Section (Optional)


 This section is used to declare variables that are accessible globally, across
all functions in the program.

Class Definition (Optional)


 The class definition section includes class data members and member
function definitions.

class MyClass
{
public:
int a;
void display()
{
cout << "Hello";
}
};

For example, a class named MyClass may contain a public integer variable ‘a’ and the
member function display() that prints "Hello" using cout.
Main Function (Mandatory)
 The main function is the entry point of a C++ program, where execution
starts.
 If the program includes classes, then the objects should be created inside
the main function. Other user-defined functions are also called from within
the main function.
Sub-program section (optional)
 It contains other part of the program. Some user defined functions are
written in this part.
Example:
// Author : abc
/* Program to demonstrate the basic structure of c++
Program */
#include<iostream.h>
using namespace std
#define PI 3.1415
int r=2;
void area();
class MyClass
{
public:
int a;
void display()
{
cout << ―Inside class‖;
}
};
int main()
{
MyClass m;
m.a =90;
[Link]();
area();
cout << ―Hello world from main function ― << endl;
cout << m.a << endl;
return 0;
}
void area()
{
float area;
area = PI * r* r;
std::cout << area << std :: endl;
}
Input & Output in C++

In C++, cin and cout are predefined objects used for input and
output operations respectively. They are defined in the iostream
header file.
cin – Input Object
 cin is used to take input from the user.
 It uses the extraction operator (>>) to receive input.
 Similar to scanf() in C.
Syntax:
cin >> variable;
Example:
int age;
cin >> age;

Note: >> is called the extraction operator because it extracts data from the
input stream and stores it in a variable.

cout – Output Object


 cout is used to display output to the screen.
 It uses the insertion operator (<<) to send output.
 Similar to printf() in C.
Syntax:
cout << "message";
Example:
cout << "Welcome to C++!";

Note: << is called the insertion operator because it inserts data into the
output stream (typically the screen).

Namespace in C++

 In a program, there are a lot of statements (i.e., lines). These statements are
divided according to the logic.
 The divided set of statements can be referred by its name. If we want to call a
function or object in another set of statements, we need to use it. This can be
done by calling its name.
 Namespace is used to identify these sets of statements.
 using and namespace are keywords.
 std is the namespace where ANSI C++ standard class libraries are defined.
 The namespace given for the object cout is std. So, we need to use it also.
 The namespace std is not user-defined; it is inbuilt.
 We need to give it along with <iostream>. Then only the object cout will be
recognized by the compiler, and it will execute successfully.

You might also like