SlideShare a Scribd company logo
Object-Oriented Programming
• Objects can be used effectively to represent
real-world entities
• For instance, an object might represent a
particular employee in a company
• Each employee object handles the processing
and data management related to that
employee
04/27/15 MCA-114 2
Objects
• An object has:
– state - descriptive characteristics
– behaviors - what it can do (or what can be done to it)
• The state of a bank account includes its account number and
its current balance
• The behaviors associated with a bank account include the
ability to make deposits and withdrawals
• Note that the behavior of an object might change its state
04/27/15 MCA-114 3
Classes
• An object is defined by a class
• A class is the blueprint of an object
• Multiple objects can be created from the same class
04/27/15 MCA-114 4
Objects and Classes
04/27/15 MCA-114 5
Bank
Account
A class
(the concept)
John’s Bank Account
Balance: $5,257
An object
(the realization)
Bill’s Bank Account
Balance: $1,245,069
Mary’s Bank Account
Balance: $16,833
Multiple objects
from the same class
Attributes
Contain current state of an object.
• Attributes can be classified as simple or complex.
• Simple attribute can be a primitive type such as
integer, string, etc., which takes on literal values.
• Complex attribute can contain collections and/or
references.
• Reference attribute represents relationship.
• complex object: contains one or more complex
attributes
04/27/15 MCA-114 6
Methods and messages
Method: Defines behavior of an object, as a set of encapsulated functions.
Message: Request from one object to another asking second object to
execute one of its methods.
04/27/15 MCA-114 7
(a)
(b)
(a) Object showing attributes and methods
(b) Example of a method
Classes
Class: Blueprint for defining a set of similar objects.
Objects in a class are called instances.
04/27/15 MCA-114 8
Data Encapsulation and Abstraction
• The wrapping up of data and functions into a single unit is known as
encapsulation. Data encapsulation is the most striking feature of a class.
The data is not accessible to the outside world and only those functions
which are wrapped in the class can access it. These functions provide the
interface between the object’s data and the program. This insulation of the
data form direct access by the program is called data hiding or information
hiding.
• Abstraction refers to the act of representing essential features without
including the background details or explanations. Classes use the concept of
abstraction and are defined as a list of abstract attributes such as size, weight
and cost and functions to operate on these attributes. They encapsulate all
the essential properties of the objects that are to be created. The attributes
are sometimes called data members because they hold information. The
functions that operate on these data are sometimes called methods or
member function. Since the classes use the concept of data abstraction, they
are knows as Abstract Data Types (ADT).
04/27/15 MCA-114 9
Inheritance
• One class can be used to derive another via
inheritance
• Classes can be organized into hierarchies
04/27/15 MCA-114 10
Bank
Account
Account
Charge
Account
Savings
Account
Checking
Account
Inheritance (contd)
Inheritance allows one class of objects to be defined as a special case
of a more general class.
Special cases are subclasses and more general cases are superclasses.
04/27/15 MCA-114 11
Generalization: process of forming a superclass
Specialization: forming a subclass
•Subclass inherits all properties of its superclass and can define its own unique
properties.
•Subclass can redefine inherited methods.
•All instances of subclass are instances of superclass.
•Principle of substitutability: instance of subclass can be used whenever method/construct
expects instance of superclass.
•A KIND OF (AKO): Name for relationship between subclass and superclass
Types of inheritance
04/27/15 MCA-114 12
(a)
(b)
(c)
(a) Single
(b) Multiple
(c) Repeated
(b)
Inheritance (contd)
• Define humanBeing to be a class
– A humanBeing has attributes, such as age, height,
gender
– Assign values to attributes when describing object
• Define Parent to be a subclass of HumanBeing
– A Parent has all attributes of a HumanBeing, plus
attributes of his/her own (name of oldest child,
number of children)
– A Parent inherits all attributes of humanBeing
• The property of inheritance is an essential feature of
object-oriented languages such as Smalltalk, C++,
Ada 95, Java (but not C, FORTRAN)
04/27/15 MCA-114 13
Inheritance (contd)
• UML notation
– Inheritance is represented by a large open triangle
04/27/15 MCA-114 14
Overriding and Overloading
Overriding: Process of redefining a property within a subclass.
Overloading: Allows name of a method to be reused with a class or across
classes.
04/27/15 MCA-114 15
Overriding Example:
Might define method in Staff class to increment salary based on commission
method void giveCommission(float branchProfit) {
salary = salary + 0.02 * branchProfit; }
May wish to perform different calculation for commission in Manager subclass:
method void giveCommission(float branchProfit) {
salary = salary + 0.05 * branchProfit; }
Aggregation
• UML Notation
04/27/15 MCA-114 16
Association
• UML Notation
04/27/15 MCA-114 17
Polymorphism and Dynamic Binding
• Polymorphism is another important OOP concept. Polymorphism, a Greek
term, means the ability to take more than one form. An operation may exhibit
different behaviors in difference instances. The behaviors depends upon the
types of data used in the operation. For example, consider the operation of
addition. For two numbers, the operation will generate a sum. If the operands
are strings , the operation would produce a third string by concatenation. The
process of making an operator to exhibit different behaviors in different
instances is known as operator overloading.
04/27/15 MCA-114 18
Polymorphism and Dynamic Binding (contd)
04/27/15 MCA-114 19
Shape
Draw()
Triangle object
Draw()
Box
Draw()
Circle
Draw()
Dynamic Binding
• Binding refers to the linking of a procedure call to the code to be executed
in response to the call. Dynamic binding means that the code associated
with a given procedure call is now knows until the time of the call at run-
time. It is associated with polymorphism and inheritance. A function call
associated with a polymorphic reference depends on the dynamic type of
that reference.
• Consider the procedure “draw”, every object will have this procedure. Its
algorithm is, however, unique to each object and so the draw procedure
will be redefined in each class that defines the object. At run-time, the
code matching the object under current reference will be called.
04/27/15 MCA-114 20
Message Passing
• An object-oriented program consists of a set of objects that communicate
with each other. The process of programming in an object oriented
language, therefore involves the following basic steps:
1. Creating classes that define objects and their behavior,
2. Creating objects from class definitions and
3. Establishing communication among objects.
• Objects communicate with one another by sending and receiving
information much the same way as people pass messages to one another.
The concept of message passing makes it easier to talk about building
systems that directly model their real-world counterparts.
• Message passing involves specifying the name of the object, the name of
the function (message) and the information to be sent. Example:-
• employee.salary(name)
• Object message information
04/27/15 MCA-114 21
Default Parameter Value
• C++ allows us to call a function without specifying all its arguments. In such
cases, the function assigns a default value to the parameter which does not have a
matching argument in the function call. Default values are specified when the
function is declared. The compiler looks at the prototype to see how many
arguments a function uses and alerts the program for possible default values.
• A default argument is checking for type at the time of declaration and evaluated at
the time of call. Once important point to note is that only the trailing arguments
can have default values and therefore we must add defaults form right to left. We
cannot provide a default value to a particular argument in the middle of an
argument list.
• Default arguments are useful in situations where some arguments always have the
same value.
04/27/15 MCA-114 22
int main()
{
float amount;
float value(float p, int n, float r=0.15);
amount=value(5000.00,5);
cout<<amount;
return 0;
}
float value(float p, int n, float r)
{
}
04/27/15 MCA-114 23
Default Parameter ValueDefault Parameter Value
Using Reference variables with
Functions
• Provision of the reference variables in C++ permits us to pass parameters to the
functions by reference. When we pass arguments by reference, the “formal”
arguments in the called function become aliases to the “actual” arguments in the
calling function. This means that when the function is working with its own
arguments, it is actually working on the original data.
• In C , this is accomplished using pointers .
void swap(int &a, int &b)
{
int t=a;
a=b;
b=t;
}
swap(m,n);
04/27/15 MCA-114 24
04/27/15 MCA-114 25
Debugging Exercises
1. What will happen when you execute the following code?
#include<iostream>
int main()
{
int i=0;
i=400*400/400;
cout<<i;
return 0;
}
2. Identify the error in the following program
#incude<iostream>
int main()
{
int num[]={1,2,3,4,5,6};
num[1]==[1]num ?cout<<“Success”:cout<<“Error”;
return 0;
}
04/27/15 MCA-114 26
Debugging Exercises
3. Identify the errors in the following program.
#include<iostream>
int main()
{
int i=5;
while(1)
{
Switch(i )
{
default:
case 4:
case 5:
break;
case 1:
continue;
case 2:
case 3:
break;
}
i--;
} }
04/27/15 MCA-114 27
Debugging Exercises
4. Identify the error in the following program.
#include<iostream>
#define pi 3.14
int squareArea(int &);
int circleArea(int &);
int main()
{
int a=10;
cout<<squareArea(a)<<“ “;
cout<<CircleArea(a)<<“ “;
cout<<a<<endl;
}
int squareArea(int &a)
{
return a*==a;
}
int circleArea(int &r)
{
return r=pi*r*r;
}
04/27/15 MCA-114 28
Debugging Exercises
5. Identify the error in the following program.
#include<iostream>
#include<cmalloc>
char* allocateMemory();
int main()
{
char *str;
str=allocateMemory();
cout<<str;
delete str;
str= “ “;
cout<<str;
}
Char* allocateMemory()
{
Str=“memory allocation test, “;
retrun str;
}
04/27/15 MCA-114 29
Debugging Exercises
6. Identify the error in the following program
#include<iostream>
void display(const int const1=5)
{
const int const2=5;
int arrary1[const1];
int arrary2[const2];
for(int i=0;i<5;i++)
{
arrary1[i]=I;
array2[i]=i*10;
cout<<array1[i]<<‘ ‘ <<array2[i]<<‘ ‘;
}
}
int main()
{
display(5);
}
04/27/15 MCA-114 30
Debugging Exercises
7. Identify the error in the following program.
#include<iostream>
int gValue=10;
void extra()
{
cout<<gValue<<‘ ‘;
}
int main()
{
extra();
{
int gValue=20;
cout<<gValue<<‘ ‘;
cout<<:gValue<<‘ ‘;
}
}
04/27/15 MCA-114 31
Find out Output
8. #include<iostream>
class user
{
private:
int i;
float f;
char c;
public:
void displaydata()
{
cout<<endl<<i<<‘n’<<f<<“n”<<c;
}
};
int main()
{
cout<<sizeof(user);
user u1;
cout<<endl<<sizeof(u1);
u1.displaydata(); }
04/27/15 MCA-114 32
Find out Output
9. #include<iostream>
class date
{
private:
int dd,mm,yy;
public:
date()
{
cout<<endl<<“Reached here”;
}
};
int main()
{
date today;
date *p=&today;
cout<<endl<<p;
}

More Related Content

PDF
Inheritance
PPTX
Java Chapter 04 - Writing Classes: part 4
PPT
C++ classes
PDF
Structures in c++
PPT
Chapter 2 java
PDF
A COMPLETE FILE FOR C++
PPTX
Java Chapter 04 - Writing Classes: part 2
PPT
Inheritance
Java Chapter 04 - Writing Classes: part 4
C++ classes
Structures in c++
Chapter 2 java
A COMPLETE FILE FOR C++
Java Chapter 04 - Writing Classes: part 2

What's hot (20)

PPT
Object-Oriented Programming Using C++
PDF
Classes and objects
PDF
C++ Object oriented concepts & programming
PDF
Constructors destructors
PDF
Object oriented concepts
PPT
358 33 powerpoint-slides_2-functions_chapter-2
PDF
Scala for Machine Learning
PPTX
DataWeave 2.0 Language Fundamentals
PPTX
class and objects
PPT
11 Using classes and objects
PPTX
Oop c++class(final).ppt
PPT
358 33 powerpoint-slides_1-introduction-c_chapter-1
PPT
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
PPT
Object oriented programming using c++
PPT
358 33 powerpoint-slides_7-structures_chapter-7
PDF
Method overloading, recursion, passing and returning objects from method, new...
PPTX
Classes and objects in c++
PDF
Object Oriented Programming With C++
PPSX
Semantic Analysis using Wikipedia Taxonomy
PDF
Introduction to database-ER Model
Object-Oriented Programming Using C++
Classes and objects
C++ Object oriented concepts & programming
Constructors destructors
Object oriented concepts
358 33 powerpoint-slides_2-functions_chapter-2
Scala for Machine Learning
DataWeave 2.0 Language Fundamentals
class and objects
11 Using classes and objects
Oop c++class(final).ppt
358 33 powerpoint-slides_1-introduction-c_chapter-1
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Object oriented programming using c++
358 33 powerpoint-slides_7-structures_chapter-7
Method overloading, recursion, passing and returning objects from method, new...
Classes and objects in c++
Object Oriented Programming With C++
Semantic Analysis using Wikipedia Taxonomy
Introduction to database-ER Model
Ad

Viewers also liked (7)

PDF
OOPs difference faqs-3
PDF
Chapter1 Introduction to OOP (Java)
PPTX
Object Orinted Programing(OOP) concepts \
PPTX
object oriented programming(syed munib ali 11b-023-bs)
PPTX
Object Oriented Concept
PDF
Implementation of oop concept in c++
PDF
Introduction to oops concepts
OOPs difference faqs-3
Chapter1 Introduction to OOP (Java)
Object Orinted Programing(OOP) concepts \
object oriented programming(syed munib ali 11b-023-bs)
Object Oriented Concept
Implementation of oop concept in c++
Introduction to oops concepts
Ad

Similar to implementing oop_concept (20)

PPT
ObjectOrientedSystems intrduction and .ppt
PPT
ObjectOrientedSystems.ppt
PPTX
3_ObjectOrientedSystems.pptx
PPTX
chapterOne.pptxFSdgfqdzwwfagxgghvkjljhcxCZZXvcbx
DOC
Introduction to OOPs Concept- Features, Basic concepts, Benefits and Applicat...
PPTX
Basic concept of oops
PPTX
PDF
L1-Introduction to OOPs concepts.pdf
PDF
Unit_2.00000000000000000000000000000.pdf
PPTX
C++ & Data Structure - Unit - first.pptx
PPTX
Object oriented programming 6 oop with c++
PPTX
object oriented programming language in c++
PDF
UNIT1- OBJECT ORIENTED PROGRAMMING IN JAVA- AIML IT-SPPU
DOC
Chapter1
PPTX
Unit - I Intro. to OOP Concepts and Control Structure -OOP and CG (2024 Patte...
PPTX
Introduction to Object Oriented Programming
PPT
Object-oriented concepts
PPTX
Rajib Ali Presentation on object oreitation oop.pptx
ObjectOrientedSystems intrduction and .ppt
ObjectOrientedSystems.ppt
3_ObjectOrientedSystems.pptx
chapterOne.pptxFSdgfqdzwwfagxgghvkjljhcxCZZXvcbx
Introduction to OOPs Concept- Features, Basic concepts, Benefits and Applicat...
Basic concept of oops
L1-Introduction to OOPs concepts.pdf
Unit_2.00000000000000000000000000000.pdf
C++ & Data Structure - Unit - first.pptx
Object oriented programming 6 oop with c++
object oriented programming language in c++
UNIT1- OBJECT ORIENTED PROGRAMMING IN JAVA- AIML IT-SPPU
Chapter1
Unit - I Intro. to OOP Concepts and Control Structure -OOP and CG (2024 Patte...
Introduction to Object Oriented Programming
Object-oriented concepts
Rajib Ali Presentation on object oreitation oop.pptx

Recently uploaded (20)

PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
PPT on Performance Review to get promotions
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
OOP with Java - Java Introduction (Basics)
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Construction Project Organization Group 2.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
DOCX
573137875-Attendance-Management-System-original
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
UNIT 4 Total Quality Management .pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Lecture Notes Electrical Wiring System Components
CH1 Production IntroductoryConcepts.pptx
PPT on Performance Review to get promotions
CYBER-CRIMES AND SECURITY A guide to understanding
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
OOP with Java - Java Introduction (Basics)
Mechanical Engineering MATERIALS Selection
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Construction Project Organization Group 2.pptx
R24 SURVEYING LAB MANUAL for civil enggi
573137875-Attendance-Management-System-original
UNIT-1 - COAL BASED THERMAL POWER PLANTS
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
UNIT 4 Total Quality Management .pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Lecture Notes Electrical Wiring System Components

implementing oop_concept

  • 1. Object-Oriented Programming • Objects can be used effectively to represent real-world entities • For instance, an object might represent a particular employee in a company • Each employee object handles the processing and data management related to that employee 04/27/15 MCA-114 2
  • 2. Objects • An object has: – state - descriptive characteristics – behaviors - what it can do (or what can be done to it) • The state of a bank account includes its account number and its current balance • The behaviors associated with a bank account include the ability to make deposits and withdrawals • Note that the behavior of an object might change its state 04/27/15 MCA-114 3
  • 3. Classes • An object is defined by a class • A class is the blueprint of an object • Multiple objects can be created from the same class 04/27/15 MCA-114 4
  • 4. Objects and Classes 04/27/15 MCA-114 5 Bank Account A class (the concept) John’s Bank Account Balance: $5,257 An object (the realization) Bill’s Bank Account Balance: $1,245,069 Mary’s Bank Account Balance: $16,833 Multiple objects from the same class
  • 5. Attributes Contain current state of an object. • Attributes can be classified as simple or complex. • Simple attribute can be a primitive type such as integer, string, etc., which takes on literal values. • Complex attribute can contain collections and/or references. • Reference attribute represents relationship. • complex object: contains one or more complex attributes 04/27/15 MCA-114 6
  • 6. Methods and messages Method: Defines behavior of an object, as a set of encapsulated functions. Message: Request from one object to another asking second object to execute one of its methods. 04/27/15 MCA-114 7 (a) (b) (a) Object showing attributes and methods (b) Example of a method
  • 7. Classes Class: Blueprint for defining a set of similar objects. Objects in a class are called instances. 04/27/15 MCA-114 8
  • 8. Data Encapsulation and Abstraction • The wrapping up of data and functions into a single unit is known as encapsulation. Data encapsulation is the most striking feature of a class. The data is not accessible to the outside world and only those functions which are wrapped in the class can access it. These functions provide the interface between the object’s data and the program. This insulation of the data form direct access by the program is called data hiding or information hiding. • Abstraction refers to the act of representing essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight and cost and functions to operate on these attributes. They encapsulate all the essential properties of the objects that are to be created. The attributes are sometimes called data members because they hold information. The functions that operate on these data are sometimes called methods or member function. Since the classes use the concept of data abstraction, they are knows as Abstract Data Types (ADT). 04/27/15 MCA-114 9
  • 9. Inheritance • One class can be used to derive another via inheritance • Classes can be organized into hierarchies 04/27/15 MCA-114 10 Bank Account Account Charge Account Savings Account Checking Account
  • 10. Inheritance (contd) Inheritance allows one class of objects to be defined as a special case of a more general class. Special cases are subclasses and more general cases are superclasses. 04/27/15 MCA-114 11 Generalization: process of forming a superclass Specialization: forming a subclass •Subclass inherits all properties of its superclass and can define its own unique properties. •Subclass can redefine inherited methods. •All instances of subclass are instances of superclass. •Principle of substitutability: instance of subclass can be used whenever method/construct expects instance of superclass. •A KIND OF (AKO): Name for relationship between subclass and superclass
  • 11. Types of inheritance 04/27/15 MCA-114 12 (a) (b) (c) (a) Single (b) Multiple (c) Repeated (b)
  • 12. Inheritance (contd) • Define humanBeing to be a class – A humanBeing has attributes, such as age, height, gender – Assign values to attributes when describing object • Define Parent to be a subclass of HumanBeing – A Parent has all attributes of a HumanBeing, plus attributes of his/her own (name of oldest child, number of children) – A Parent inherits all attributes of humanBeing • The property of inheritance is an essential feature of object-oriented languages such as Smalltalk, C++, Ada 95, Java (but not C, FORTRAN) 04/27/15 MCA-114 13
  • 13. Inheritance (contd) • UML notation – Inheritance is represented by a large open triangle 04/27/15 MCA-114 14
  • 14. Overriding and Overloading Overriding: Process of redefining a property within a subclass. Overloading: Allows name of a method to be reused with a class or across classes. 04/27/15 MCA-114 15 Overriding Example: Might define method in Staff class to increment salary based on commission method void giveCommission(float branchProfit) { salary = salary + 0.02 * branchProfit; } May wish to perform different calculation for commission in Manager subclass: method void giveCommission(float branchProfit) { salary = salary + 0.05 * branchProfit; }
  • 17. Polymorphism and Dynamic Binding • Polymorphism is another important OOP concept. Polymorphism, a Greek term, means the ability to take more than one form. An operation may exhibit different behaviors in difference instances. The behaviors depends upon the types of data used in the operation. For example, consider the operation of addition. For two numbers, the operation will generate a sum. If the operands are strings , the operation would produce a third string by concatenation. The process of making an operator to exhibit different behaviors in different instances is known as operator overloading. 04/27/15 MCA-114 18
  • 18. Polymorphism and Dynamic Binding (contd) 04/27/15 MCA-114 19 Shape Draw() Triangle object Draw() Box Draw() Circle Draw()
  • 19. Dynamic Binding • Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding means that the code associated with a given procedure call is now knows until the time of the call at run- time. It is associated with polymorphism and inheritance. A function call associated with a polymorphic reference depends on the dynamic type of that reference. • Consider the procedure “draw”, every object will have this procedure. Its algorithm is, however, unique to each object and so the draw procedure will be redefined in each class that defines the object. At run-time, the code matching the object under current reference will be called. 04/27/15 MCA-114 20
  • 20. Message Passing • An object-oriented program consists of a set of objects that communicate with each other. The process of programming in an object oriented language, therefore involves the following basic steps: 1. Creating classes that define objects and their behavior, 2. Creating objects from class definitions and 3. Establishing communication among objects. • Objects communicate with one another by sending and receiving information much the same way as people pass messages to one another. The concept of message passing makes it easier to talk about building systems that directly model their real-world counterparts. • Message passing involves specifying the name of the object, the name of the function (message) and the information to be sent. Example:- • employee.salary(name) • Object message information 04/27/15 MCA-114 21
  • 21. Default Parameter Value • C++ allows us to call a function without specifying all its arguments. In such cases, the function assigns a default value to the parameter which does not have a matching argument in the function call. Default values are specified when the function is declared. The compiler looks at the prototype to see how many arguments a function uses and alerts the program for possible default values. • A default argument is checking for type at the time of declaration and evaluated at the time of call. Once important point to note is that only the trailing arguments can have default values and therefore we must add defaults form right to left. We cannot provide a default value to a particular argument in the middle of an argument list. • Default arguments are useful in situations where some arguments always have the same value. 04/27/15 MCA-114 22
  • 22. int main() { float amount; float value(float p, int n, float r=0.15); amount=value(5000.00,5); cout<<amount; return 0; } float value(float p, int n, float r) { } 04/27/15 MCA-114 23 Default Parameter ValueDefault Parameter Value
  • 23. Using Reference variables with Functions • Provision of the reference variables in C++ permits us to pass parameters to the functions by reference. When we pass arguments by reference, the “formal” arguments in the called function become aliases to the “actual” arguments in the calling function. This means that when the function is working with its own arguments, it is actually working on the original data. • In C , this is accomplished using pointers . void swap(int &a, int &b) { int t=a; a=b; b=t; } swap(m,n); 04/27/15 MCA-114 24
  • 24. 04/27/15 MCA-114 25 Debugging Exercises 1. What will happen when you execute the following code? #include<iostream> int main() { int i=0; i=400*400/400; cout<<i; return 0; } 2. Identify the error in the following program #incude<iostream> int main() { int num[]={1,2,3,4,5,6}; num[1]==[1]num ?cout<<“Success”:cout<<“Error”; return 0; }
  • 25. 04/27/15 MCA-114 26 Debugging Exercises 3. Identify the errors in the following program. #include<iostream> int main() { int i=5; while(1) { Switch(i ) { default: case 4: case 5: break; case 1: continue; case 2: case 3: break; } i--; } }
  • 26. 04/27/15 MCA-114 27 Debugging Exercises 4. Identify the error in the following program. #include<iostream> #define pi 3.14 int squareArea(int &); int circleArea(int &); int main() { int a=10; cout<<squareArea(a)<<“ “; cout<<CircleArea(a)<<“ “; cout<<a<<endl; } int squareArea(int &a) { return a*==a; } int circleArea(int &r) { return r=pi*r*r; }
  • 27. 04/27/15 MCA-114 28 Debugging Exercises 5. Identify the error in the following program. #include<iostream> #include<cmalloc> char* allocateMemory(); int main() { char *str; str=allocateMemory(); cout<<str; delete str; str= “ “; cout<<str; } Char* allocateMemory() { Str=“memory allocation test, “; retrun str; }
  • 28. 04/27/15 MCA-114 29 Debugging Exercises 6. Identify the error in the following program #include<iostream> void display(const int const1=5) { const int const2=5; int arrary1[const1]; int arrary2[const2]; for(int i=0;i<5;i++) { arrary1[i]=I; array2[i]=i*10; cout<<array1[i]<<‘ ‘ <<array2[i]<<‘ ‘; } } int main() { display(5); }
  • 29. 04/27/15 MCA-114 30 Debugging Exercises 7. Identify the error in the following program. #include<iostream> int gValue=10; void extra() { cout<<gValue<<‘ ‘; } int main() { extra(); { int gValue=20; cout<<gValue<<‘ ‘; cout<<:gValue<<‘ ‘; } }
  • 30. 04/27/15 MCA-114 31 Find out Output 8. #include<iostream> class user { private: int i; float f; char c; public: void displaydata() { cout<<endl<<i<<‘n’<<f<<“n”<<c; } }; int main() { cout<<sizeof(user); user u1; cout<<endl<<sizeof(u1); u1.displaydata(); }
  • 31. 04/27/15 MCA-114 32 Find out Output 9. #include<iostream> class date { private: int dd,mm,yy; public: date() { cout<<endl<<“Reached here”; } }; int main() { date today; date *p=&today; cout<<endl<<p; }