2. www.SunilOS.com 2
C++ is a Programming Language
C++ is a programming language.
just like any other primitive language such
as C, Pascal.
It has
o Variables
o Functions
o Data Type
o Control Statement
o Arrays
3. www.SunilOS.com 3
C++ is OOP
3 Idiot
C++ is Object Oriented Programming .
follows OOP methodology.
C++ thinks only Objects.
Meri
4 Lakh
ki watch
Just like a Money Oriented Person
who always thinks of Money.
4. www.SunilOS.com 4
Basic Unit of C++ is Object
Such as program of
o sum of two numbers is an object
o Fibonacci Series is an object
o SMS Services is an object
o Email Services is an object
o Account Services is an object
Basic unit of C++ is an Object.
5. Expert Object
Each Object is an Expert object.
Expert object contains related variables and
functions.
www.SunilOS.com 5
6. An Expert never overlaps responsibilities
www.SunilOS.com 6
Creator
Preserver
Destroyer
Trimurti
8. www.SunilOS.com 8
Object has State & Behavior
Object has state and behavior.
State will be changed by behavior.
9. www.SunilOS.com 9
Object has State & Behavior
States are stored in memory variables.
Behavior changes states.
Behaviors are implemented by functions;
functions are referred as methods in OOP
Variables and Methods of an object are
defined by Class.
Class is the structure or skeleton of an
Object.
10. Class vs Objects
www.SunilOS.com 10
Realization
Realization
State/Variables
currentGear
Speed
Color
Methods
changeGear()
Accelerator()
break()
changeColor()
State/Variables
name
address
Methods
changeName()
changeAddress()
Design
Real world entities based
on design
11. Class is the basic building block
The basic building block of C++ is a Class.
Also known as C with Classes.
C++ program is nothing but a Class.
C++ application is made of Classes.
www.SunilOS.com 11
13. Executable Program
An executable Program must have default
method ‘main’ .
Method main() is the entry point of a
Program.
main() is where program execution begins.
main() is called at runtime.
www.SunilOS.com 13
17. www.SunilOS.com 17
C++ Program with Class
#include<iostream.h>
#include<conio.h>
class HelloCPlusPlus {
…
};
void main(){…….}
A class may contain multiple variables and
methods.
A Class should have default ‘main’ method.
18. www.SunilOS.com 18
My First Program – Hello C++
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<“Hello C++”;
getch();
}
19. www.SunilOS.com 19
Keywords
class – is used to define a class.
public – Access modifier shows accessibility of a
class or variable or method to other classes. There
are 3 access modifiers public, protected and
private.
static – Memory for the static variables is
assigned only once in life. Non-static variables are
called instance variables.
void – is a NULL return type of main method.
20. www.SunilOS.com 20
Methods & Header Files
cout console output is used to write output at
standard device.
getch() method is used to get character.
clrscr() is used to clear console screen
clrscr() and getch() both are predefined function in
"conio.h" (console input output header file).
“iostream.h” Header that defines the standard
input/output stream objects:
21. www.SunilOS.com 21
Compile & Run Program
Save program by .cpp extension
Compile program by
o ALT+f9
Execute program by
o CTL+f9
23. COUT & CIN
cout is used to print output at console.
cout<<“Enter Your Age”;
cin is used to get the data from console.
cin>>age;
Extraction operator >>
Insertion operator <<
www.SunilOS.com 23
32. www.SunilOS.com 32
C++ Data Types
Data Types:
o bool true or false
o char 1 byte
o int 4 byte
o float 4 byte
o double 8 byte
o short int 2 byte
o long int 8 byte
o wchar_t 4 byte (wide character)
33. extern
extern keyword is used to declare a variable
at any place.
Though you can declare a variable multiple
times in your C++ program
It can be defined only once in a file, a
function or a block of code.
www.SunilOS.com 33
34. extern int a, b;
extern int c;
extern float f;
int main () {
// Variable definition:
int a, b; int c; float f;
// actual initialization
a = 10; b = 20;
c = a + b;
}
www.SunilOS.com 34
36. Constant
Fixed value that the program may not alter.
Two ways to define constant
o Using #define preprocessor.
o Using const keyword.
o outside main
#define LENGTH 10
#define WIDTH 5
o inside main
const int LENGTH = 10;
const int WIDTH = 5;
www.SunilOS.com 36
38. Functions & Purpose
strcpy(s1,s2) – copies string s2 into s1
strcat(s1,s2) – concat string s2 onto end
of s1
strlen(s1) – return length of string
strcmp(s1, s2) - Returns 0 if s1 and s2 are
the same; less than 0 if s1<s2; greater than 0 if
s1>s2.
www.SunilOS.com 38
46. www.SunilOS.com 46
Define an Array
int table[10][9];
table[1][5] = 5;
Passing function to array
o void functionName(int array_name[5]){
…………
…………
o }
47. Pointers
Pointer is a variable whose value is the
address of another variable.
Pointer Variable declaration
o datatype *var_name;
o int *ip;
It stores the address of variable.
www.SunilOS.com 47
48. Operations with Pointer Variable
int var = 20; // actual variable declaration.
int *ip; // pointer variable
ip = &var; // store address of var in pointer variable
cout << var << endl; // print the address stored in ip pointer
variable
cout << ip << endl; // access the value at the address
available in pointer
cout << *ip << endl;
www.SunilOS.com 48
49. www.SunilOS.com 49
Function Prototype
int sum (int, int);
int main () {
o int total;
o total = sum (2, 3);
o printf ("Total is %dn", total);
o return 0;
}
int sum (int a, int b)
o {
return a + b;
o }
50. www.SunilOS.com 50
Return a Value
double getDivision(int a, int b)
o {
double div = a / b;
return div;
o }
}
51. Disclaimer
This is an educational presentation to enhance the
skill of computer science students.
This presentation is available for free to computer
science students.
Some internet images from different URLs are
used in this presentation to simplify technical
examples and correlate examples with the real
world.
We are grateful to owners of these URLs and
pictures.
www.SunilOS.com 51