Lecture 1-3
CSE202: OBJECT ORIENTED PROGRAMMING
Outline
• Introduction of C++ Programming
• Differences between Procedural and Object-Oriented
Programming
• Features of Input/Output Streams
• Reading and Writing Data
• Class and Object
Concepts and Basics of C++ Programming :
C++, as we all know is an extension to C language and was
developed by Bjarne stroustrup at bell labs.
C++ is a general-purpose programming language and widely used
nowadays for competitive programming. It has imperative,
object-oriented and generic programming features. C++ runs on
lots of platform like Windows, Linux, Unix, Mac, etc.
However, to become proficient in the C ++ programming language,
one must first understand the fundamental differences between
procedural and object-oriented programming paradigms.
Features of Input/Output Streams
Here we will discuss one simple and basic C++ program to print
"Hello this is C++" and its structure in parts with details and uses.
Header files are included at the beginning just like in C program. Here iostream is a header
file which provides us with input & output streams. Header files contained predeclared
function libraries, which can be used by users for their ease.
Using namespace std, Consider a situation, when we have two persons with the same
name, ABC, in the same class. Whenever we need to differentiate them definitely we would
have to use some additional information along with their name, like second name
(PQR,STU..), rollno etc.
•Same situation can arise in your C++ applications. For example, you might be writing
some code that has a function called abc() and there is another library available which is
also having same function abc(). Now the compiler has no way of knowing which version
of abc() function you are referring to within your code.
A namespace is designed to overcome this difficulty and is used as additional
information to differentiate similar functions, classes, variables etc. with the same
name available in different libraries. Using namespace, you can define the context in
which names are defined. In essence, a namespace defines a scope.
Therefore, Namespace collects identifiers used for class,
object and variables. NameSpace can be used by two
ways in a program, either by the use of using statement
at the beginning, like we did in above mentioned
program or by using name of namespace as prefix before
the identifier with scope resolution (::) operator.
Examples:
Using namespace std;
cout<<“A”; std::cout << "A";
cout <<, is used to print anything on screen, same as printf in C
language. cin and cout are same as scanf and printf, only
difference is that you do not need to mention format specifiers
like, %d for int etc, in cout & cin.
Example:
int a=10;
printf(“a= %d”,a); (In C programming)
cout<<“a=“<<a; (In C++ programming)
Cout/cin will discuss later
The file iostream includes
A.The streams of includes and outputs of program effect.
B.The declarations of the basic standard input-output library.
C.both a and b
D.none of these
The file iostream includes
A.The streams of includes and outputs of program effect.
B.The declarations of the basic standard input-output library.
C.both a and b
D.none of these
Reading and Writing Data
Two operators are introduced in c++ i.e. cout and cin.
cout is a predefined object and represents the standard output stream and this
output stream represents the screen.
eg. cout<<“Hello”;
cout will display this string as such on screen.
<< is called insertion operator.
if “a’ is variable then cout can be used to display the contents of “a”.
cout<< a;
To print the numbers and character variables:
Notes:
•The endl manipulator is used to insert a new line. That's why each output is
displayed in a new line.
•The << operator can be used more than once if we want to print different variables,
strings and so on in a single statement. For example:
cout << "character: " << ch << endl;
cin is a predefined object and represents the standard input
stream and this input stream is used to read the data or take value
from user.
eg: int a,b;
cin>>a>>b;
>> is called extraction operator.
In C++, cin takes formatted input from standard input devices such as
the keyboard.
Example: Enter and Display Integer Value
Note: If we don't include the using namespace std; statement, we need to
use std::cin instead of cin .
Taking Multiple Inputs: cin>>a>>b>>c;
The operator used with cout is called:
a. Insertion operator
b. Extraction operator
c. Get operator
d. Comparison operator
The operator used with cout is called:
a. Insertion operator
b. Extraction operator
c. Get operator
d. Comparison operator
The operator used with cin is called:
a. Insertion operator
b. Extraction operator
c. Get operator
d. Comparison operator
The operator used with cin is called:
a. Insertion operator
b. Extraction operator
c. Get operator
d. Comparison operator
In CPP, cin and cout are the predefined stream __________ .
a. Operator
b. Functions
c. Objects
d. Data types
In CPP, cin and cout are the predefined stream __________ .
a. Operator
b. Functions
c. Objects
d. Data types
Classes in CPP are________ .
a. derived data types
b. User defined data types
c. built-in data types
d. All of these
Classes in CPP are________ .
a. derived data types
b. User defined data types
c. built-in data types
d. All of these
Classes in CPP are________ .
a. derived data types
b. User defined data types
c. built-in data types
d. All of these
C++ practicing examples
Program to Find Largest Number Among Three Numbers
Program to Calculate Sum of Natural Numbers
Program to Check Whether a Number is Prime or Not
Program to Display Fibonacci Series
Class and objects
In C++, rather than creating separate variables and functions, we can
also wrap these related data and functions in a single place (by
creating objects). This programming paradigm is known as object-
oriented programming.
But before we can create objects and use them in C++, we first need to
learn about classes.
Class
A class is a blueprint for the object.
We can think of a class as a sketch (prototype) of a house. It contains all the
details about the floors, doors, windows, etc. Based on these descriptions we
build the house. House is the object.
Create a Class
A class is defined in C++ using keyword
class followed by the name of the class. class className
The body of the class is defined inside {
the curly brackets and terminated by a “;” at the end. // some data
// some functions
};
Class: A class in C++ is the building block, that leads to
Object-Oriented programming. It is a user-defined data type,
which holds its own data members and member functions,
which can be accessed and used by creating an instance of that
class..
For Example: Consider the Class of Cars. There may be many
cars with different names and brand but all of them will share
some common properties like all of them will have 4
wheels, Speed Limit, cost etc.
What does your class can hold?
A. data
B. functions
C. both a & b
D. none of the mentioned
What does your class can hold?
A. data
B. functions
C. both a & b
D. none of the mentioned
An Object is an instance of a Class. When a class is defined, no
memory is allocated but when it is instantiated (i.e. an object is
created) memory is allocated.
When a class is defined, only the
specification for the object is
defined; no memory or storage is
allocated. To use the data and
access functions defined in the
class, you need to create objects.
Syntax:
ClassName ObjectName;
Accessing a Data Member
Accessing a data member depends solely on the access control of
that data member. If its public, then the data member can be
easily accessed using the direct member access (.) operator with
the object of that class.
class A
{
public: Access Specifier : will discuss in
the next slide
int x;
Void func();
}obj1; 1st Method to declare object
main()
{
A obj2; 2nd Method to declare object
obj1.x=10;
obj2.x=20; Obj_name.Var1
obj1.func(); Obj_name.function()
Accessing data members using obj
---
}
C++ supports three access specifiers:
public
private
protected
The public access specifier allows a class to subject its member variables and
member functions to other functions and objects.
The private access specifier allows a class to hide its member variables and
member functions from other class objects and functions. (Default Access Specifier)
The protected access specifier allows a class to hide its member variables and
member functions from other class objects and functions just like private access
specifier - is used while implementing inheritance.
will discuss
it after MTT
Area of a circle
// C++ program to demonstrate public // C++ program to demonstrate private
// access modifier // access modifier
#include <iostream> #include <iostream>
using namespace std; using namespace std;
class Circle {
// class definition private:
class Circle { double radius;
public: public:
double radius; void compute_area(double r)
double compute_area() {
{ radius = r;
return 3.14 * radius * radius; double area = 3.14 * radius * radius;
} cout << "Radius is: " << radius << endl;
}; cout << "Area is: " << area;
int main() }
{ };
Circle obj; int main()
// accessing public data member outside class {
obj.radius = 5.5; Circle obj;
obj.radius = 5.5;
cout << "Radius is: " << obj.radius << "\n"; // Error trying to access private data member
cout << "Area is: " << obj.compute_area(); // directly outside the class
return 0;
} obj.compute_area(1.5);
return 0;
}
The default access level assigned to members of a class is
___________
a)Private
b)Public
c)Protected
d)Needs to be assigned
The default access level assigned to members of a class is
___________
a)Private
b)Public
c)Protected
d)Needs to be assigned
We cannot use __________ members outside the class
a. Public
b. private
c. Protected
d. Local
We cannot use __________ members outside the class
a. Public
b. private
c. Protected
d. Local
PUBLIC PRIVATE
The class members declared as private can
All the class members declared under
be accessed only by the functions inside
public will be available to everyone.
the class. will discuss
it later
The data members and member functions Only the member functions or the friend
declared public can be accessed by other functions are allowed to access the
classes too. private data members of a class.
The public members of a class can be
They are not allowed to be accessed
accessed from anywhere in the program
directly by any object or function outside
using the direct member access operator (.)
the class.
with the object of that class.
Find output:
class Circle {
int radius;
float compute_area()
{ a: 3.140000
radius=1; b: 3.14
return 3.14 * radius * radius; c: no output
} d: error
};
int main()
{
Circle obj;
cout << "Area is: " << obj.compute_area();
return 0;
}
Find output:
class Circle {
int radius;
float compute_area()
{ a: 3.140000
radius=1; b: 3.14
return 3.14 * radius * radius; c: no output
} d: error
};
int main() Note: Pvt.
{ member can’t
access outside
Circle obj;
the class
cout << "Area is: " << obj.compute_area();
return 0;
}
Methods definition
• The member function of the class can be defined in two
different ways:
1) Inside the class definition:- The member functions are simple
defined inside the class only i.e the body of the function resides
inside the range of class only.
2) Outside the class definition: by using scope resolution
operator, which specifies that the scope of the function is
restricted to the class class_name.
Syntax:- class_name:: function_name
Inside the class definition
Eg: class abc
{ cout<<“rollno=“;
private: cin>>rollno;
int rollno; }
char name[20]; void display()
public: {
void getdata() cout<<“name=“<<name;
{ cout<<“rollno=“<<rollno;
cout<<“name=“; }
cin>>name; };
Outside the class definition
Example:
cin>>name;
class abc
cout<<“rollno=“;
{
cin>>rollno;
private:
}
int rollno;
void abc :: display()
char name[20];
{
public:
cout<<“name and rollno=“;
void getdata();
cout<<name<<rollno;
void display();
}
};
void abc :: getdata()
{
cout<<“name=“;
C++ practicing examples
Class to represent the details of a student
Class to represent the details of two students
Class to represent the ‘n’ student details.
More points
Size of the empty class in C++ is 1 byte(Reason: Unique address
identification for objects)
Memory allocation for objects:
Memory allocation for objects