Introduction To C++: Johra Muhammad Moosa
Introduction To C++: Johra Muhammad Moosa
Computer Programming
Introduction to C++
Prepared by
Johra Muhammad Moosa
Assistant Professor
CSE, BUET
Modified by
Madhusudan Basak
Assistant Professor
CSE, BUET
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 written • If no parameter void is optional
• Function prototype optional • Functions must be prototyped
• return ; in a function with return type • function with return type other than void
other than void is possible must return a value
• Return type default to int if not • Return type of function must be declared
specified explicitly
• Local variables can be declared
• Local variable can be declared only at
anywhere
the start of the block
• 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