Unit 1.1
Unit 1.1
S
• Introduction to C++ Programming
• Features of object-oriented programming
• Difference between object oriented and
procedure-oriented programming
5
Introduction to C++ Programming
•The prime purpose of C++ programming was to add object
orientation to the C programming language, which is
in itself one of the most powerful programming languages.
8
Obje
• ct
Object is a instance of class.
• Represent a person, a place, a bank account, a table of data or any item
the program has to handle.
4
Cla
• ss
Set of data & code of an object ->user-defined data type –>class.
• Once a class has been defined, we can create any number of objects belonging to
that class.
• A class is collection of objects of similar type.
• Syntax used to create an object is no different than the syntax used to create an
integer object in C.
5
Data Abstraction &
Encapsulation
• Wrapping up of data & functions into a single unit (called class) -encapsulation.
• Data is not accessible to the outside world
• Functions which are wrapped in the class can access it.
• Abstraction - act of representing essential features without background details.
Fig.2. Encapsulation
6
Information
Hiding
• Insulation of data from direct access by the program – data or information hiding.
• Attributes are - data members because they hold information.
• Functions are - member functions.
7
Inheritan
ce
• Process by which objects of one class acquire the properties of objects of another
class.
• Each derived class shares common characteristics with the class from which it is
derived(fig.4).
• Inheritance provides reusability and expandability.
Fig. 5. Polymorphism 9
Difference between object
oriented and procedure-
oriented
Procedure-oriented programming
programming Object-oriented programming
Emphasis is on doing things. Emphasis is on data rather than procedure.
Large programs divided into smaller functions(fig.1). Programs are divided into objects.
Functions can share global data(fig.2). Functions that operate on the data of an object are
tied together in the data structure.
Data move openly. Data is hidden (fig.3).
Functions transform data from one form to another. Objects may communicate with each other through
functions.
Top-down approach in program design. Bottom-up approach in program design.
15
Summary
16
Difference between structure and class
• The language, through which user can interact with computer is
known as computer language or programming language.
Class Structure
Usage Generally used for large amounts of data. Generally used for smaller amounts of data. 13
Data
Types
The table below shows the fundamental data
types, their meaning, and their sizes (in bytes):
Data Type Meaning Size (in Bytes)
int Integer 2 or 4
float Floating-point 4
char Character 1
wchar_t Wide Character 2
bool Boolean 1
void Empty 0
14
1. C++ int
The int keyword is used to indicate integers.
Its size is usually 4 bytes. Meaning, it can store values from -2147483648 to 2147483647.
For example,
int salary = 85000;
As mentioned above, these two data types are also used for exponentials. For example,
double distance = 45E12 // 45E12 is equal to 45*10^12
15
3. C++ char
Keyword char is used for characters.
Its size is 1 byte.
Characters in C++ are enclosed inside single quotes ' '.
For example,
char test = 'h’;
4. C++ bool
The bool data type has one of two possible values:
true or false.
Booleans are used in conditional statements and loops (which we will learn
in later chapters).
For example,
bool cond = false;
16
5. C++ void
The void keyword indicates an absence of data. It means "nothing" or "no
value".
We will use void when we learn about functions and pointers.
Note: We cannot declare variables of the void type.
17
C++ Type
Modifiers
We can further modify some of the fundamental data types by using type
modifiers. There are 4 type modifiers in C++. They are:
signed
unsigned
short
Long
We can modify the following data types with the above modifiers:
int
double
char
18
Data Type Size (in Bytes) Meaning
signed int 4 used for integers (equivalent to int)
unsigned int 4 can only store positive integers
20
Input and output streams
(cin, cout)
• C++ comes with libraries that provide us with many ways for
performing input and output. In C++ input and output are performed
in the form of a sequence of bytes or more commonly known as
streams.
• Cout in C++ and cin in C++ are used very often for printing outputs and
taking inputs respectively. These two are the most basic methods of
taking input and printing output in C++. To use cin and cout in C++ one
must include the header file iostream in the program.
22
Standard output stream (cout):
• Usually the standard output device is the display screen.
• The C++ cout statement is the instance of the ostream class.
• It is used to produce output on the standard output device which is usually the
display screen.
• The data needed to be displayed on the screen is inserted in the standard output
stream (cout) using the insertion operator(<<).
• Example:
#include <iostream>
using namespace std;
int main()
{
char sample[] =
"GeeksforGeeks";
cout << sample <<
" - A computer
science portal for
geeks";
return 0; 23
Standard input stream (cin):
• Usually the input device in a computer is the keyboard.
• C++ cin statement is the instance of the class istream and is used to read input
from the standard input device which is usually a keyboard.
• The extraction operator(>>) is used along with the object cin for reading
inputs.
• The extraction operator extracts the data from the object cin which is entered
using the keyboard.
• Example:
#include <iostream>
using namespace std;
int main()
{
int age;
cout << "Enter
your age:";
cin >> age;
cout << "\nYour age is: " << age;
return 0;
}
24
Summary
29
Namesp
•ace
Namespaces provide a method for preventing name
conflicts in large projects.
• Symbols declared inside a namespace block are
placed in a named scope that prevents them from
being mistaken for identically-named symbols in
other scopes.
• Multiple namespace blocks with the same name are
allowed. All declarations within those blocks are
declared in the named scope.
26
Defining a
Namespace
• A namespace definition begins with the
keyword namespace followed by the namespace
name as follows −
29
Example:
#include <iostream>
using namespace std;
// first name space
namespace first_space {
Output:
void func() { Inside first_space
cout << "Inside
first_space" << endl;
}
}
// second name space
namespace
second_space {
void func() {
cout << "Inside
second_space" <<
endl;
}
}
using namespace
first_space;
int main () {
// This calls function
from first name space.
func(); 30
Summary
35