Professional Documents
Culture Documents
OBJECT-ORIENTED
PROGRAMMING
• STRUCTURE • ARRAY
INTEGRAL VOID FLOATING
• CLASS • FUNCTION
• UNION • POINTER
• ENUMERATION
3
INT CHAR FLOAT DOUBLE
• Reference Variables:- C++ introduces a new kind of variable known as the reference variable. A
reference variable provides an alias for a previously defined variable.
• A reference variable is created as follows:
datatype & reference-name = variable-name;
Variables that are declared inside a function or block are local variables. They can be used only by
statements that are inside that function or block of code. Local variables are not known to functions
outside their own. Following is the example using local variables −
#include <iostream>
int main ()
{
int a, b; int c; // Local variable declaration:
a = 10; b = 20; // actual initialization
c = a + b;
cout << c;
return 0;
}
6
Global variables are defined outside of all the functions, usually on top of the program. The global
variables will hold their value throughout the life-time of your program.
A global variable can be accessed by any function. That is, a global variable is available for use
throughout your entire program after its declaration. Following is the example using global and local
variables −
#include <iostream>
int main ()
{
int a, b; // Local variable declaration:
a = 10; b = 20; // actual initialization
g = a + b;
cout << g;
return 0;
7
}
• RELATIONAL OPERATOR
• INCREMENT/DECREMENT OPERATOR
• LOGICAL OPERATOR
• BITWISE OPERATOR
SUBTRACTION - X -Y x minus y
MULTIPLICATION * X *Y x multiplied by y
DIVISION / X /Y x divided y
MODULUS % X %Y x divided y
(remainder)
11
ASSUME X=5
X IS INCREMENTED BEFORE EVALUTION
EXPRESSION X=1+X
A=++X
A=6
12
ASSUME X=5
X IS INCREMENTEDAFTER EVALUTION
EXPRESSION X=X+1
A=X++
A=5
13
14
BINARY XOR ^ A^B Operator copies the bit if it is set in one operand
but not both.
BINARY ONE ~ ~A Operator is unary and has the effect of 'flipping'
COMPLEMENT bits.
BINARY RIGHT SHIFTER >> A>> The left operands value is moved right by the
number of bits specified by the right operand.
BINARY LEFT SHIFTER << A<< The left operands value is moved left by the
number of bits specified by the right operand.
15
16
A = 0011 1100 A = 0011 1100 A = 0011 1100 A = 0011 1100 A = 0011 1100 A = 0011 1100
B = 0000 1101 B = 0000 1101 B = 0000 1101
------------------- ------------------- ------------------- ------------------- ------------------- -------------------
=0000 1100 =0011 1101 =0011 0001 =1100 0011 =0000 1111 =1111 0000
17
18
A conditional branch is a statement that causes the program to change the path of execution based on the
value of an expression. The most basic conditional branch is if statement.
if (Condition)
statements1;
else
statements2;
Example:-
void main()
{
int no;
cout << "Enter a number: ";
cin >> no;
if (no > 10)
cout << no << "is greater than 10" << endl;
else
cout << no << "is not greater than 10" << endl;
}
19
void main()
{
int no;
cout << "Enter a number: ";
cin >> no;
if (no > 10)
cout << no << "is greater than 10" << endl;
else if (no < 5)
cout << no << "is less than 5" << endl;
else
cout << no << "is between 5 and 10" << endl;
}
20
void main()
{
int no;
cout << "Enter a number: ";
cin >> no;
if (no > 10)
{
if (no < 20)
cout << no << "is between 10 and 20" << endl;
else cout << no << "is greater than 20" << endl;
}
}
21
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a
case, and the variable being switched on is checked for each case Syntax:-
..
switch(expression)
{
case constant-expression :
statement(s);
break; //Optional
case constant-expression :
statement(s);
break; //optional
default ://Optional
statement(s);
22
}
24
A while loop statement repeatedly executes a target statement as long as a given condition is true.
Syntax:-
while (expression)
{
STATEMENTS;
}
STATEMENTS;
25
return 0;
}
26
A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least
one time Syntax:-
.
d0{
statements;
}
while(condition);
27
int main () {
// Local variable declaration: value of a: 10
int a = 10; value of a: 11
value of a: 12
// do loop execution value of a: 13
do { value of a: 14
cout << "value of a: " << a << endl; value of a: 15
a = a + 1; value of a: 16
} while( a < 20 ); value of a: 17
value of a: 18
return 0; value of a: 19
}
28
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute
a specific number of times Syntax:-
.
29
• Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the
loop does not execute and flow of control jumps to the next statement just after the for loop.
• After the body of the for loop executes, the flow of control jumps back up to the increment statement.
This statement can be left blank, as long as a semicolon appears after the condition.
• The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body
of loop, then increment step, and then again condition). After the condition becomes false, the for loop
terminates
30
31
32
return result;
}
34
35
36
return 0;
} 37
38
39
// function declaration
// function definition to swap the void swap(int x, int y);
values.
void swap(int x, int y) { int main () {
int temp; // local variable declaration:
int a = 100;
temp = x; /* save the value of x */ int b = 200;
x = y; /* put y into x */
y = temp; /* put x into y */ cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
return;
} // calling a function to swap the values.
swap(a, b);
41
42
// function declaration
void swap(int &x, int &y);
// function definition to swap the
int main () {
values. // local variable declaration:
void swap(int &x, int &y) { int a = 100;
int temp; int b = 200;
temp = x; /* save the value at
address x */ cout << "Before swap, value of a :" << a << endl;
x = y; /* put y into x */ cout << "Before swap, value of b :" << b << endl;
y = temp; /* put x into y */
/* calling a function to swap the values using variable
return; reference.*/
} swap(a, b);
return 0;
By:- Prof. Prajakta Deepak Shetye }
OUTPUT:-
44
45
46
return 0; 49
}
50
// function declaration
void swap(int *x, int *y);
// function definition to swap the values.
void swap(int *x, int *y) { int main () {
int temp; // local variable declaration:
temp = *x; /* save the value at address x */ int a = 100;
*x = *y; /* put y into x */ int b = 200;
*y = temp; /* put x into y */
cout << "Before swap, value of a :" << a << endl;
return; cout << "Before swap, value of b :" << b << endl;
}
/* calling a function to swap the values.
* &a indicates pointer to a ie. address of variable a and
* &b indicates pointer to b ie. address of variable b.
*/
swap(&a, &b);
Actually, you do not place the null character at the end of a string constant. The C++ compiler
automatically places the '\0' at the end of the string when it initializes the array.
53
void main ()
{
When the above code is compiled and executed, it produces the following result −
• strcmp(s1, s2); -- Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
• strchr(s1, ch); -- Returns a pointer to the first occurrence of character ch in string s1.
• strstr(s1, s2); -- Returns a pointer to the first occurrence of string s2 in string s1.
55
void main () {
ENCAPSULATI
INHERITANCE
ON
58
• CLASS :- Class is way to bind data and its associated functions together.
59
The class declaration describes the type and scope of its members. The class function definitions describes
how class functions are implemented.
60
};
61
• Accessing a global variable when there is a local variable with same name
• Defining a function outside a class
• Accessing a class’s static variables
• Referring to a class inside another class
• In case of multiple Inheritance
63
64
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
double getVolume(void)
{
return length * breadth * height;
}
};
65
double Box::getVolume(void)
{
return length * breadth * height;
}
Here, only important point is that you would have to use class name just before :: operator.
66
68
void display()
{
area= 3.14*radius*radius;
cout<<“Area of circle is”<<area;
}
69
};
By:- Prof. Prajakta Deepak Shetye
OBJECT
• An object is a variable whose datatype is a class.
• User can have more than one object for a class
• The object can be declared as
class-name oject1_name ,object2_name;
The declaration of object is similar to the basic variable.
• The private data of a class can be accessed only through the member functions of that class using the
direct member access operator (.).
• The public data of a class can be accessed using the direct member access operator (.).
70
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
72
}
};
By:- Prof. Prajakta Deepak Shetye
class Circle
void Circle :: getdata()
{
{
private: cout<<“enter the radius”;
float radius, area; //data member cin>>radius;
public: }
void getdata(); // 1st member function
void Circle :: calculate()
void calculate(); //2nd member function {
area=3.14*radius*radius;
void print(); //3 rd member function }
};
Void Circle ::print()
{
Void main() cout<<“Area of circle is”<<area’
{ }
Circle c1;
c1.getdata();
c1.calculate();
c1.print(); 74
}
By:- Prof. Prajakta Deepak Shetye
class Circle
{ Void main()
private: {
float radius, area; Circle c1;
public:
void getdata(float rad) c1.getdata(12.5);
{ c1.calculate();
radius=rad; c1.print();
} }
void calculate()
{
area= 3.14*radius*radius;
}
void print()
{
cout<<“Area of circle is”<<area;
75
}
};
By:- Prof. Prajakta Deepak Shetye
Class Simple
#include<iostream.h>
{
#include<conio.h>
double si,prin,rate,time;
Void main()
public:
{
void getdata(double p, double r, double t);
double si,prin,rate,time;
void calculate();
void display();
cout<<“Enter the values for prin, rate, time”;
};
cin>>prin>>rate>>time;
Void Simple::getdata(double p, double r, double t)
{
si=(prin*rate*time)/100;
prin=p;
rate=r;
cout<<“The simple interest is”<<si;
time=t;
}
Void main() }
{ void Simple:: calculate()
Simple s1; {
s1.getdata(5000.0,2.5,12.0); si=(prin*rate*time)/100;
S1.calculate(); }
S1.display(); void display()
} { 76
cout<<“Simple interest”<<si;
}
By:- Prof. Prajakta Deepak Shetye
FRIEND FUNCTION
A friend function of a class is defined outside that class' scope but it has the right to access all private and
protected members of the class. Even though the prototypes for friend functions appear in the class
definition, friends are not member functions.
A friend can be a function, or member function, or a class, in which case the entire class and all of its
members are friends. C++ called friend functions that break this rule and allow us to access member
functions from outside the class.
To declare a function as a friend of a class, precede the function prototype in the class definition with
keyword friend as follows –
class Box
{
double width;
public:
double length; 77
friend void printWidth( Box box );
By:- Prof. Prajakta Deepak Shetye
void setWidth( double wid );
};
NEED OF FRIEND FUNCTION
78
79
abc.get();
void exchange (B&x, A&y) pqr.getdata();
{
int temp; cout<<“Before exchange”;
temp= x.b;
x.b= y.a; abc.show();
y.a=temp; pqr.display();
}
exchange(abc,pqr);
cout<<“After exchange”;
abc.show();
pqr.display();
81
}
}
By:- Prof. Prajakta Deepak Shetye
STATIC DATA MEMBER
• It is initialized to zero when the first object of its class is #include <iostream.h>
created. No other initialization is permitted. void Test()
{
• Only one copy of that member is created for entire class static int x = 1;
and is shared by all objects of that class, no matter how x = ++x;
many objects are created.
int y = 1;
• It is visible only within the class, but its lifetime is the
y = ++y;
entire program.
cout<<"x = "<<x<<"n";
cout<<"y = "<<y<<"n";
}
int main()
{
Test();
Test(); 83
return 0;
By:- Prof. Prajakta Deepak Shetye
}
int Example :: x;
#include <iostream.h>
void main()
class Example
{
{
Example obj1, obj2, obj3;
static int x;
public:
cout<<"Initial value of x" <<"n";
void function1()
{
obj1.function2();
x++;
obj2.function2();
}
obj3.function2();
void function2()
obj1.function1();
{
obj2.function1();
cout<<"x = "<<x<<"n";
obj3.function1();
}
};
cout<<"Value of x after calling function1"<<"n";
obj1.function2(); 84
obj2.function2();
By:- Prof. Prajakta Deepak Shetye
obj3.function2();
}
1 2 3
Static int
x=0
X++
X=1
X=2
X=3 x=3
85
Ratio::Ratio()
86
{ m=1; n=2;
}
By:- Prof. Prajakta Deepak Shetye
CHARCTERISTICS
• The constructor name is all way same as class name.
• They do not have return type, not even void.
• They cannot be static or virtual.
• They should be declared in public section.
• They cannot be inherited though derived class can called base class constructor.
• They can have default arguments like c++ functions.
• We cannot refer to their address.
• When a constructor is declared for a class, initialization of class objects became mandatory, since
constructor is invoked automatically when the objects are created.
87
88
getch();
} 91
Fibonacci::Fibonacci()
{
f0=0;
f1=1;
}
92
93
99
C2
X=1.1
y=1.7
C3=
C1+ c2
10
1
The above makes SAMPLE have access to both public and protected variables of base class RATIO
}
By:- Prof. Prajakta Deepak Shetye
SHAPE CLASS
RECTANGLE CLASS
PUBLIC: SETWIDTH()
SETHEIGHT()
PUBLIC: GETAREA()
PROTECTED: WIDTH
HEIGHT
RECTANGLE CLASS
AFTER INHERIT
PUBLIC SECTION
GETAREA()
SETWIDTH()
SETHEIGHT()
11
0
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
return 0;
}
114
Class Student
115
Class Result
By:- Prof. Prajakta Deepak Shetye
class Student class Test : public Student
{ {
int roll_no; public:
float t1,t2;
public: void get_marks(float x,float y)
void get_no(int a) {
{ t1=x;
roll_no=a; t2=y;
} }
void put_no(void) void put_marks(void)
{ {
cout<<“Roll No:-”<<roll_n0; cout<<“Marks Obtained”<<t1<<”\t”<<t2<<endl;
} }
}; };
116
};
117
res.get_no(101);
res.get_marks(39,65);
Output:-
res.get_score(29);
Roll No:- 101
res.display();
Marks Obtain:- 39 65
Sports Marks:- 29
getch();
Total Marks Obtain:- 133
}
118
119
ANIMALSOUND()
DERIVED 1 DERIVED 2
DOG CLASS PIG CLASS
ANIMALSOUND() ANIMALSOUND()
ANIMAL SOUND ANIMAL SOUND
() ()
12
0
122
Output:
Output: 30
Output: 66 12
3
124
12
6
12
7
12
8
Output:
13
0
INPUT STREAM
TO HANDLE ALL INPUT AND OUTPUT OPERTION WE NEED
TO INCLUDE fstream.h HEADER FILE IN ALL FILE
HANDLING PROGRAM.
PROGRAM
13
1
13
2
Here, the first argument specifies the name and location of the file to be opened and the second argument
of the open() member function defines the mode in which the file should be opened.
13
4
You can combine two or more of these values by ORing them together. For example if you want to open a file in write
mode and want to truncate it in case that already exists, following will be the syntax −
ofstream outfile;
outfile.open("file.dat", ios::out | ios::trunc );
Similar way, you can open a file for reading and writing purpose as follows −
fstream afile;
afile.open("file.dat", ios::out | ios::in ); 13
5
• Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects.
void close();
13
6
• While doing C++ programming, you write information to a file from your program using the stream insertion
operator (<<) just as you use that operator to output information to the screen. The only difference is that you use
an ofstream or fstream object instead of the cout object.
• You read information from a file into your program using the stream extraction operator (>>) just as you use that
operator to input information from the keyboard. The only difference is that you use an ifstream or fstream object
instead of the cin object.
13
7
Above examples make use of additional functions from cin object, like getline() function to read the line from
outside and ignore() function to ignore the extra characters left by previous read statement.
13
9
Syntax
Stream_object.eof( );
Example :
fout.eof( );
14
0
e.g file.get(ch);
e.g. file.put(ch);
e.g file.getline(s,80);
We can also use file>>ch for reading and file<<ch writing in text file. But >> operator does not accept
white spaces.
14
1