0% found this document useful (0 votes)
25 views31 pages

Unit 1.1

The document provides an introduction to C++ programming, highlighting its object-oriented features and differences from procedure-oriented programming. Key concepts include classes, objects, data abstraction, encapsulation, inheritance, and polymorphism. Additionally, it covers fundamental data types, input/output operations, and the use of namespaces in C++.
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)
25 views31 pages

Unit 1.1

The document provides an introduction to C++ programming, highlighting its object-oriented features and differences from procedure-oriented programming. Key concepts include classes, objects, data abstraction, encapsulation, inheritance, and polymorphism. Additionally, it covers fundamental data types, input/output operations, and the use of namespaces in C++.
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/ 31

CONTENT

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.

•The core of the pure object-oriented programming is to


create an object, in code, that has certain properties and
methods. While designing C++ modules, we try to see
whole world in the form of objects.

•For example a car is an object which has certain properties


such as color, number of doors, and the like. It also has
certain methods such as accelerate, brake, and so on.
6
Properties of C++
Programming
•Data as well as operations are encapsulated in objects
•Information hiding is used to protect internal properties of
an object
•Objects interact by means of message passing.
•In most object-oriented languages objects are grouped
in classes
• Objects in classes are similar enough to
programming of the classes, as opposed to
allow
programming of the individual objects
• Classes represent concepts whereas objects
represent phenomena
•Classes are organized in inheritance hierarchies.
for class extension or specialization
Provides 7
Features of Object-Oriented
Programming
•Object
•Class
•Data abstraction and Encapsulation
•Information hiding
•Inheritance
•Polymorphism

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.

Fig.1. Two ways of representing an object

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.

Example-- If fruit has been defined as a class, then the statement


fruit mango;
will create an object mango belonging to the class fruit.

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.

Fig.3. Concept of Information Hiding

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. 4. Property Inheritance


8
Polymorphi
sm to take more than one form.
• Ability
• An operation may exhibit different behaviours in different instances.
• Using a single function name to perform different types of tasks is known
as
function overloading(fig.5).

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

In this lecture we have We have discussed about some


discussed about C++ features of C++
programming language.

Moreover, we have learnt


about difference between
Procedure-Oriented and
Object-Oriented Programming

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

A class in C++ can be defined as a collection of related


variables and functions encapsulated in a single A structure can be referred to as a user defined data
Definition type possessing its own operations.
structure.

Keyword for the declaration Class Struct


Default access specifier Private Public
class myclass struct myclass
{ {
private: private:
int data; int data;
public: public:
myclass(int data_): myclass(int data_):
Example data(data_) data(data_)
{} {}
virtual void foo()=0; virtual void foo()=0;
virtual ~class() virtual ~class()
{} {}
}; };

Purpose Data abstraction and further inheritance Generally, grouping of data

Type Reference Value

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

double Double Floating-point 8

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;

2. C++ float and double


float and double are used to store floating-point numbers (decimals and exponentials).
The size of float is 4 bytes and the size of double is 8 bytes. Hence, double has two times
the precision of float. To learn more, visit C++ float and double.
For example,
float area = 64.74;
double volume = 134.64534;

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

short 2 used for small integers (range -


32768 to 32767)

long at least 4 used for large integers (equivalent


to long int)
used for large positive integers or 0
unsigned long 4 (equivalent to unsigned long int)
used for very large integers
long long 8 (equivalent to long long int).
used for very large positive integers
unsigned long long 8 or 0 (equivalent to unsigned long
long int)
long double 12 used for large floating-point
numbers
used for characters (guaranteed
signed char 1 range -127 to 127)

unsigned char 1 used for characters (range 0 to 255)


2
3
Derived Data
Types
• Data types that are derived from fundamental data types are derived
types. For example: arrays, pointers, function types, structures, etc.

• We will learn about these derived data types in later tutorials.

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.

• Input Stream: If the direction of flow of bytes is from the device(for


example, Keyboard) to the main memory then this process is called
input.
• Output Stream: If the direction of flow of bytes is opposite, i.e. from
main memory to device( display screen ) then this process is called
output.
21
Header files available in C++ for Input/Output operations are:

• iostream: iostream stands for standard input-output stream. This header


file contains definitions to objects like cin, cout, cerr etc.
• iomanip: iomanip stands for input output manipulators. The methods
declared in this files are used for manipulating streams. This file contains
definitions of setw, setprecision, etc.
• fstream: This header file mainly describes the file stream. This header file is
used to handle the data being read from a file as input or data being
written into the file as output.

• 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

In this lecture we have We have discussed about


discussed about difference various data types in C+
between structure and class. +

Moreover we have learnt


about basic i/o in c++

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 −

namespace namespace_name { // code declarations }

• To call the namespace-enabled version of either


function or variable, prepend (::) the namespace
name as follows −

name::code; // code could be variable or function.


27
Example:
#include <iostream>
using namespace std;
// first name space
namespace first_space {
Output:
void func() {
Inside first_space
cout << "Inside
Inside second_space
first_space" << endl;
}
}
// second name space
namespace second_space {
void func() {
cout << "Inside
second_space" << endl;
}
}
int main () {
// Calls function from first name space.
first_space::func();

// Calls function from second name


space.
second_space::func();
return 0;
}
28
The using
directive
• You can also avoid prepending of namespaces with the using
namespace directive. This directive tells the compiler that the
subsequent code is making use of names in the specified namespace.
• The ‘using’ directive can also be used to refer to a particular item
within a namespace. For example, if the only part of the std
namespace that you intend to use is cout, you can refer to it as
follows −
using std::cout;

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

In this lecture we have discussed about Namespaces in C++

35

You might also like