0% found this document useful (0 votes)
37 views23 pages

Introduction To C++: Johra Muhammad Moosa

This document provides an introduction to object-oriented programming in C++. It discusses key OOP concepts like encapsulation, polymorphism, and inheritance. Encapsulation binds code and data together into objects. Polymorphism allows one name to have multiple purposes. Inheritance allows an object to acquire properties of another object. The document also covers C++ features like namespaces, which avoid name collisions, and function overloading and inlining. It provides examples of simple C++ programs and differences between C and C++.

Uploaded by

Aye Thein Maung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views23 pages

Introduction To C++: Johra Muhammad Moosa

This document provides an introduction to object-oriented programming in C++. It discusses key OOP concepts like encapsulation, polymorphism, and inheritance. Encapsulation binds code and data together into objects. Polymorphism allows one name to have multiple purposes. Inheritance allows an object to acquire properties of another object. The document also covers C++ features like namespaces, which avoid name collisions, and function overloading and inlining. It provides examples of simple C++ programs and differences between C and C++.

Uploaded by

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

CSE 109

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 

You might also like