0% found this document useful (0 votes)
14 views

CSE 109 Lecture 8

Uploaded by

aranya13078
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

CSE 109 Lecture 8

Uploaded by

aranya13078
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Object Oriented Programing

• Encourages to decompose a problem into constituent parts


• An object is the composition of nouns (like data such as
numbers, strings, or variables) and verbs (like actions, such
as functions).
Features of OOP
• Encapsulation
• Polymorphism
• Inheritance
Encapsulation
• Binds the code and the data it manipulates together
• An object is a variable of a user defined type
Polymorphism
• One name, many purpose
• In C: abs(), labs(), fabs()
• In C++: one function abs()
• Can be applied to
– Methods
– Operators
Inheritance
• One object can acquire the properties of another
C++
• Superset of C
• Supports OOP
Simple program
#include<iostream> #include<stdio.h>
using namespace std;
int main()
int main() {
{ /*program code*/
/*program code*/ return 0;
return 0; }
}
Simple program
#include<iostream> #include<stdio.h>
using namespace std;
int main()
int main() {
{ /*program code*/
/*program code*/ return 0;
return 0; }
}
New Style Header
• Do not specify file names
• Standard identifiers: may be mapped to files by the
compiler
• Actually abstractions – just guarantee that appropriate
prototypes and definitions exist
• .h is not needed
• Example:
– #include<cmath>
– #include<cstring>
• Old style headers are also supported
– But obsolete
Simple program
#include<iostream> #include<stdio.h>
using namespace std; int main()
int main() {
{ /*program code*/
/*program code*/ return 0;
return 0; }
}
Namespaces
• When new style header is included the content of the
header are contained in the std namespace
• Namespace: a declarative region
• To localize the name of the identifiers to avoid collision
• To bring the std namespace into visibility
– using namespace std;
Console I/O
• The output operatot <<
• The input operator >>
• cout is a predefined stream automatically linked to stdout
– Possible to output any C++ basic type
– cout<<expression;
– cout<<3.1416;
– cout<<3-1;
• cin is a predefined stream automatically linked to stdin
– int num;
cin>>num;
– & not needed
– Line buffered
Console I/O
Console I/O
• For input individual data items must be separated by
whitespace characters
Differences
C C++
• If no parameter void should be • If no parameter void is
written optional
• Function prototype optional • Functions must be prototyped
• return ; in a function with • function with return type
return type other than void is other than void must return a
possible value
• Return type default to int if not • Return type of function must
specified be declared explicitly
• Local variable can be declared • Local variables can be
only at the start of the block declared anywhere
• Defines bool datatype
• Keyword true and false are
defined
Function Overloading
• Two or more function with same name
• Have to declare and define all the versions
Function Overloading
In-line function
• Function not called
• Expanded in-line at the point of each call
• No overhead associated
• Faster in execution than normal function
• If too large and called too often program grows large
• In general short functions are declared as in-line function
• Must be defined before its first call
In-line function
In-line function

• Is functionally equivalent to
References
• Teach Yourself C++ by Herbert Schildt (Third Edition)
– Chapter 1 (1.1-1.4, 1.6-1.8)
Thank You ☺

You might also like