Lecture-3-Basics of C++

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 28

Lecture-3

Basics of C++
Basics of C++

• C ++ is an object oriented programming language


• C ++ was developed by Jarney Stroustrup at AT & T Bell lab, USA in
early eighties.
• C ++ was developed from c and simula 67 language.
• C ++ was early called ‘C with classes’.
C++ Comments
•A comment is text that the compiler ignores but that is useful for
programmers.
•Comments are normally used to annotate code for future reference.
•The compiler treats them as white space.
•You can use comments in testing to make certain lines of code inactive.

C++ introduces a new comment symbol //(double slash).


Comments start with a double slash symbol and terminate at the end
of line.
A comment may start any where in the line and what ever follows till
the end of line is ignored.
Note that there is no closing symbol.
The double slash comment is basically a single line comment
Example:
// this is an example of
// c++ program
// thank you

The c comment symbols /* ….*/ are still valid and more suitable for
multi line comments.
/* this is an example of c++ program */
Output Operator
•The statement cout <<”Hello, world” displayed the string with in quotes on
the screen.
•The identifier cout can be used to display individual characters, strings and
even numbers.
•It is a predefined object that corresponds to the standard output stream.
•Stream just refers to a flow of data and the standard Output stream normally
flows to the screen display.
•The cout object, whose properties are defined in iostream.h represents that
stream.
•The insertion operator << also called the ‘put to’ operator directs the
information on its right to the object on its left.
Return Statement

•In C++ main ( ) returns an integer type value to the operating system.
•Therefore every main ( ) in C++ should end with a return (0) statement,
otherwise a warning or an error might occur.
Input Operator
•The statement cin>> number 1; is an input statement and causes the
program to wait for the user to type in a number.
•The number keyed in is placed in the variable number1.
•The identifier cin is a predefined object in C++ that corresponds to the
standard input stream.
•Here this stream represents the key board.
•The operator >> is known as get from operator.
•It extracts value from the keyboard and assigns it to the variable on its
right.
Cascading Of I/O Operator
• cout<<”sum=”<<sum<<”\n”;
• cout<<”sum=”<<sum<<”\n”<<”average=”<<average<<”\n”;
• cin>>number1>>number2;
Structure Of A Program

// my first program in C++


#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! Welcome to CGU.";
return 0;
}

Output:-Hello World! Welcome to CGU.


#include <iostream>

•Lines beginning with a hash sign (#) are directives for the preprocessor.
•They are not regular code lines with expressions but indications for the
compiler's preprocessor.
•In this case the directive #include<iostream> tells the preprocessor to
include the iostream standard file.
•This specific file (iostream) includes the declarations of the basic
standard input-output library in C++, and it is included because its
functionality is going to be used later in the program.
using namespace std

•All the elements of the standard C++ library are declared within what is
called a namespace, the
•namespace with the name std.
•So in order to access its functionality we declare with this expression
that we will be using these entities.
•This line is very frequent in C++ programs that use the standard library,
and in fact it will be included in most of the source codes included in
these tutorials.
int main ()

•This line corresponds to the beginning of the definition of the main


function.
•The main function is the point by where all C++ programs start their
execution, independently of its location within the source code.
•It does not matter whether there are other functions with other names
defined before or after it – the instructions contained within this
function's definition will always be the first ones to be executed in any
C++ program.
•For that same reason, it is essential that all C++ programs have a main
function.
STRUCTURE OF C++ PROGRAM
Include files
Class declaration
Class functions, definition
Main function program
Example:

# include<iostream.h> void display()


class person {
{ cout<<”\n name:”<<name;
cout<<”\n age:”<<age;
char name[30];
}
int age; int main( )
public: {
void getdata(void); person p;
void display(void); p.getdata();
}; p.display();
void person :: getdata ( void ) return(0);
{ }
cout<<”enter name”;
cin>>name;
cout<<”enter age”;
cin>>age;
}
TOKENS
•The smallest individual units in program are known as
tokens. C++ has the following tokens.

i. Keywords
ii. Identifiers
iii. Constants
iv. Strings
v. Operators
KEYWORDS
•The keywords implement specific C++ language feature.
•They are explicitly reserved identifiers and can’t be used as names for
the program variables or other user defined program elements.
•The keywords not found in ANSI C are shown in red letter.

asm double new switch


auto else operator template
break enum private this
case extern protected throw
catch float public try
char for register typedef
class friend return union
const goto short unsigned
continue if signed virtual
default inline sizeof void
delete long struct while
IDENTIFIERS
•Identifiers refers to the name of variable , functions, array, class etc. created by programmer.
•Each language has its own rule for naming the identifiers.
•The following rules are common for both C and C++.

1. Only alphabetic chars, digits and under score are permitted.


2. The name can’t start with a digit.
3. Upper case and lower case letters are distinct.
4. A declared keyword can’t be used as a variable name.

In ANSI C the maximum length of a variable is 32 chars but in c++ there is no bar.
Constants
•The constants in ‘C++’ are applicable to the values that do not
change during execution of a program.
Write a program to demonstrate various constants.

#include<iostream.h>
#include<conio.h>
int main()
{
int x;
float y;
char z;
double p;
clrscr();
x=20;
y=2e1;
z=‘a’;
p=3.2e20;
cout<<“x=”<<x<<endl;
cout<<“y=”<<y<<endl;
cout<<“z=”<<z<<endl;
cout<<“p=”<<p; OUTPUT
return 0; x=20
} y=20
z=a
p=3.2e+20
BASIC DATA TYPES IN C++
Functions

•A function is a self-contained block or a sub-program of one or


more statements that perform a special task when called.
•The C++ functions are more civilized than C.
•It is possible to use the same name with multiple definitions
known as function overloading.
Write a program to demonstrate user-defined functions.

#include<conio.h>
#include<iostream.h>
int main()
{
clrscr();
void show (void);
show();
return 0;
}
void show()
{
cout<<“\n In function show()”;
}
Differences between c and c++
(1) C is a procedure/function-oriented language and C++ language is driven by
a procedure/object.
(2) Data is not protected in C, whereas data is secured in C++. Data hiding
concept is absent in C.
(3) C uses a top down approach while C++ uses a bottom up approach. The
program is prepared step by step in C, and in C++ base elements are prepared
first.
(4) In C, we cannot give the same name to two functions in a program, whereas
due to the function overloading feature, the above concept is possible in C++.
One can initialize a number of functions with the same name, but with different
arguments. The polymorphism feature is built in C++, which supports this
concept.
Differences between c and c++
(5) C uses printf() and scanf() functions to write and read the data respectively,
while C++ uses cout and cin objects for output and input operations,
respectively. Further, the cout uses << (insertion operator) and cin uses >>
( extraction operator).
(6) C uses stdio.h file for input and output functions, whereas C++ uses
iostream.h for these functions.
(7) Constructor and destructors are absent in C and the same are provided in
C++.
(8) Inline functions are supported by C++, and the same are absent in C. Inline
functions can be used as micros. They are stated by a word ‘inline’.
Thank You

You might also like