Introduction to C++ and Data
Structures
Name :- Amit Kumar Jaiswal Submitted to :- Gaurav Kumar
Roll No. :- 63
CONTENT:
Very brief history of C++
Definition object-oriented programming
Object-oriented concepts
First program!
Some C++ syntax
Function calls
Very brief history of C++
S im
ula 6
7
C++
class GasMolecule
Object-oriented programming • Data:
• molecular weight, structure, common
names, etc.
Object-oriented programming • Methods:
• IR(wavenumStart, wavenumEnd) :
(OOP) seeks to define a program in return IR emission spectrum in range
terms of the things in the problem (files,
molecules, buildings, cars, people,
Objects (instances of a class)
etc.), what they need, and what they
can do. GasMolecule ch4
GasMolecule co2
“pseudo-code”
spectrum = ch4.IR(1000,3500)
Name = co2.common_name
Object-oriented programming “Class Car”
OOP defines classes to represent these
things. public interface
Classes can contain data and methods
(internal functions).
Classes control access to internal data
and methods. A public interface is used
by external code when using the class.
This is a highly effective way of modeling
real world problems inside of a computer
program.
private data and methods
“Actually I made up the term ‘object-oriented’, and I can tell you I did
Characteristics of C++ not have C++ in mind.”
– Alan Kay (helped invent OO programming, the Smalltalk language, and the GUI)
C++ is…
Compiled.
A separate program, the compiler, is used to turn C++ source code into a form directly
executed by the CPU.
Strongly typed and unsafe
Conversions between variable types must be made by the programmer (strong typing) but can
be circumvented when needed (unsafe)
C compatible
call C libraries directly and C code is nearly 100% valid C++ code.
Capable of very high performance
The programmer has a very large amount of control over the program execution
Object oriented
With support for many programming styles (procedural, functional, etc.)
No automatic memory management
The programmer is in control of memory usage
Hello, World! explained
The main routine – the start of every C++ program! It
returns an integer value to the operating system and (in
this case) takes no arguments: main()
The return statement returns an integer value to
the operating system after completion. 0 means “no
error”. C++ programs must return an integer value.
loads a header file containing function and class
definitions
Hello, World! explained
Loads a namespace called std. Namespaces are used to
separate sections of code for programmer convenience.
To save typing we’ll always use this line in this tutorial.
cout is the object that writes to the stdout device, i.e. the console
window.
It is part of the C++ standard library.
Without the “using namespace std;” line this would have been called
as std::cout. It is defined in the iostream header file.
<< is the C++ insertion operator. It is used to pass characters from
the right to the object on the left. endl is the C++ newline character.
Header Files C++ language headers aren’t referred
to with the .h suffix. <iostream>
provides definitions for I/O functions,
including the cout function.
C++ (along with C) uses header files as to
hold definitions for the compiler to use
while compiling. #include <iostream>
A source file (file.cpp) contains the code using namespace std;
that is compiled into an object file (file.o).
The header (file.h) is used to tell the int main()
{
compiler what to expect when it string hello = "Hello";
assembles the program in the linking string world = "world!";
stage from the object files. string msg = hello + " " + world ;
cout << msg << endl;
Source files and header files can refer to msg[0] = 'h';
any number of other header files. cout << msg << endl;
return 0;
}
Basic Syntax
C++ syntax is very similar to C, Java, or C#. Here’s a few things up front and we’ll cover
more as we go along.
Curly braces are used to denote a code block (like the main() function):
{ … some code … }
Statements end with a semicolon:
int a ;
a = 1 + 3 ;
Comments are marked for a single line with a // or for multilines with a pair of /* and */ :
// this is a comment.
/* everything in here
is a comment */
Variables can be declared at any time in a code block. void my_function() {
int a ;
a=1 ;
int b;
}
Functions are sections of code that are called from other code. Functions always have a
return argument type, a function name, and then a list of arguments separated by
commas:
int add(int x, int y) { // No arguments? Still need ():
int z = x + y ; void my_function() {
return z ; /* do something...
} but a void value means the
return statement can be skipped.*/
}
A void type means the function does not return a value.
// Specify the type
int x = 100;
Variables are declared with a type and a name: float y;
vector<string> vec ;
// Sometimes types can be inferred
auto z = x;
A sampling of arithmetic operators:
Arithmetic: + - * / % ++ --
Logical: && (AND) ||(OR) !(NOT)
Comparison: == > < >= <= !=
Sometimes these can have special meanings beyond arithmetic, for
example the “+” is used to concatenate strings.
What happens when a syntax error is made?
The compiler will complain and refuse to compile the file.
The error message usually directs you to the error but sometimes the error occurs before the
compiler discovers syntax errors so you hunt a little bit.
Built-in (aka primitive or intrinsic) Types
“primitive” or “intrinsic” means these types are not objects
Here are the most commonly used types.
Note: The exact bit ranges here are platform and compiler dependent!
Typical usage with PCs, Macs, Linux, etc. use these values
Variations from this table are found in specialized applications like embedded system processors.
Name Name Value Name Value
char unsigned char 8-bit integer float 32-bit floating point
short unsigned short 16-bit integer double 64-bit floating point
int unsigned int 32-bit integer long long 128-bit integer
long unsigned long 64-bit integer long double 128-bit floating point
bool true or false
THANK
YOU