Professional Documents
Culture Documents
Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP)
Programming(OOP)
1
Paradigm Shift in Programming
Programs: Data, Statements, Functions
Programming with flowchart
Program = Data Structure + Algorithms
Structured Programming
– Sequence Instructions
– Decision Making
– Looping
2
ALGORITHMS
• An algorithm is a sequence of precise instructions for
solving a problem in a finite amount of time.
Properties of an Algorithm:
8
Object-Oriented Programming
• Programming for simulation
• Software Crisis
• Software Reuse
• Software IC
• Polymorphism,Inheritance and Encapsulation (PIE).
• Classes as an Abstract Data Type(Abstraction)
• Easy to debug and maintain
• Mainstream in software development
• Software components.
9
Object Oriented Languages
class name {
attributes and
declarations symbolic constants
how to manipulate
method definitions
the state of objects
} These parts of a class can
actually be in any order
18
Sample class
#include<iostream.h>
class Pencil
{
public String color = “red”;
public int length;
public float diameter;
setcolor(string);
World
Single level
Rajasthan
Parrot Sparrow Base class
Jaipur
Multiple
Class A
Class base Class A
{
{ {
};
Data members and };
Class B:public A
Functions; Class B
{
}; {
};
Class };
Class C:public B
derived:public base Class C:public A, Public B
{
{ {
};
Data members and };
Class D: public C
functions;
{
};
};
B C D
E F G H I J
Hybrid = Hierarchical + Multiple
Class A
Class A
Class B Class C
Class B Class C I Half(Hierarchical)
Class D Class A
Hybrid
Class D
Class D
Animal
34
Using C++
• File | New…
• Projects | Win32 Console Application
– Project Name, Location
– Files | C++ Source File
– File, Location
• Edit Program
• Build | Compile ***.cpp
• Build | Build ***.exe
• Build | Execute ***.exe
• Execute and stop the program
35
C++ Character set
A – Z= 26 = 65 to 90
a – z = 26 = 97 to 122
ASCII Values
0 – 9 = 10 = 48 to 57
Symbols = 34= other values
TOTAL = 96
C++ Data types
S. No DATA TYPE Size (in bytes) RANGE
2. Logical &&,||,!
3. Relational <,>,>=,<=,==,!=
4. Assignment =
5. Increment ++
6. Decrement --
7. Comma ,
8. Conditional (Ternary) ?:
9. Bitwise &,|,^,!,>>,<<
41
C vs C++
#include <iostream.h>
void main()
{
cout <<"Hello!"<< endl;
43
Stream Input/Output
Stream Input
Keyboard INDIA
Stream Output
INDIA Console Unit
44
Program Average
#include <iostream.h>
void main() {
int x;
int y;
cout <<"Enter two numbers \n";
cin >> x >> y;
cout <<"Their average is: ";
cout << (x + y)/2.0 << endl;
}
45
Structures
#include<iostream.h>
Void main() {
struct book{
int page_no;
char name[15];
};
book b1;
b1.page_no=125;
b1.name =“oops”;
cout <<“Book Name"<< b1.name <<“Book
page no"<< b1.page_no <<endl; }
46
Call by Reference
#include <iostream.h>
#include<conio.h>
void swap(int *, int *);
void main() {
int i=7, j=-3;
Clrscr();
swap(&i,&j);
cout <<"i = "<< i << endl
<<"j = "<< j << endl;
getch();
}
void swap(int *a, int *b) {
int t;
t = *a;
*a = *b;
*b = t; 47
}
Return by reference
• Example
int val1() {
//……
return i;
}
//…… Temporary
j = val1(); storage
i 8 8 j 8
48
Matrix Multiplication
#include<iostream.h>
#include<conio.h>
void main()
{
int row1,col1,row2,col2;
int i,j,k;
int mat1[5][5],mat2[5][5],multi[5][5];
clrscr();
cout<<"enter the row for first matrix\n";
cin>>row1;
cout<<"enter column for first matrix\n";
cin>>col1;
cout<<"enter elements of first matrix\n";
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
cin>>mat1[i][j];
}
}
cout<<"enter the row of second matrix\n";
cin>>row2;
cout<<"enter column for second matrix\n";
cin>>col2;
cout<<"enter elements of second matrix\n";
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
cin>>mat2[i][j];
}
}
if(col1==row2)
{
cout<<"multiplication of matrices is\n";
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
multi[i][j]=0;
for(k=0;k<col1;k++)
{
multi[i][j]+=mat1[i][k]*mat2[k][j];
}
cout<<multi[i][j];
}
cout<<endl;
}
}
else
cout<<"multiplication is not compatible";
getch();
}
Inline Functions
#include <iostream.h>
53
Overloading
A class can have more than one method with the
same name as long as they have different parameter
list.
public class Pencil
{