C Unit-I
C Unit-I
1
Prepared By N.KANNAN/AP/CSE/EGSPEC
COURSE OUTCOMES(CO)
1902CS302- Object Oriented Programming(OOP’S)
1. CO1 Define the features of C++ supporting object oriented
programming.
7
ADVANTAGES
Example
#include <iostream.h>// header file library
using namespace std;// we can use names for objects and
variables from the standard library.
10
DECLARING (CREATING)
VARIABLES
To create a variable, specify the type and assign it a value.
Syntax:
Data_type variable_name = value;
Example1:
int age= 15;// Var Declr
cout << age;//print value
Example
2:
int age = 15;
age= 10;
cout << age; 11
C++ DECLARE
MULTIPLE
VARIABLES
To declare more than one variable of the same type,
use a comma-separated list:
Example
int x = 5, y = 6, z =
50; cout << x + y + z;
12
ONE VALUE TO MULTIPLE
VARIABLES
You can also assign the same value to multiple
variables in one line.
Example
int x, y, z;
x = y = z = 50;
cout << x + y + z;
13
C++ IDENTIFIERS
All C++ variables must be identified with unique
names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more
descriptive names (age, sum, Average).
Example
int minutesPerHour = 60; // Good
15
C++ CONSTANTS
16
C++ USER INPUT
You have already learned that cout is used to output
(print) values. Now we will use cin to get user input.
cin is a predefined variable that reads data from the
keyboard with the extraction operator (>>).
In the following example, the user can input a number,
which is stored in the variable x. Then we print the
value of x:
Example
int x;
cout << "Type a number: “;
cin >> x;
cout << "Your number is: " << x;
17
DATA TYPES
18
C++ DATA TYPES SIZE
19
KEYWORDS IN C++
20
PROGRAMMING PARADIGMS
Programming paradigms are different ways or styles in
which a given program or programming language can be
organized.
21
CHARACTERISTICS OF OOP’S
22
OBJECTS
Any entity that contains data and its related
functions.
For example, a chair, pen, table, keyboard, bike, etc.
25
TYPES OF INHERITANCE
IN C++
26
POLYMORPHISM
27
ENCAPSULATION
Meaning of Encapsulation, is to make sure
that "sensitive" data is hidden from other
users.
Binding code and data together into a
single unit are known as encapsulation.
Ex: Capsule
28
ABSTRACTION
Abstraction - Hiding internal details
and showing functionality is known as
abstraction.
Abstraction means displaying only essential
information and hiding the details.
For example phone call, we don't know the internal
processing.
Note:
Data abstraction can be used to provide security for
29
30
C++ CLASSES
A Class is a user-defined data type that has data members
and member functions.
31
CREATE A CLASS IN C++
A class is defined in C++ using keyword class followed
by the name of the class.
33
CREATE OBJECT IN C++
Syntax:
Class_name Object_name;
Example:
int main()
{
Student s1; // Create an object of Student
s1.id = 15; // Access attributes &set values
s1.name = ”Ram";
cout << s1.id << "\
n"; cout <<s1.name; // Print attribute values
return 0;
} 34
ACCESS
SPECIFIERS/MODIFIERS
35
C++
ACCESS SPECIFIERS/MODIFIERS
Access specifiers define how the data members and
methods of a class can be accessed.
36
TYPES OF ACCESS MODIFIERS
In C++, there are three access modifiers:
1. public - members are accessible from outside the
class
2. private - members cannot be accessed (or viewed)
from outside the class
3. protected - members cannot be accessed from
outside the class, however, they can be accessed in
inherited classes.
37
PUBLIC ACCESS MODIFIER
The public keyword is used to create public members
(data and functions).
The public members are accessible from any part of
the program.
SYNTAX:
{
// data member
// member functions
}; 38
Example 1: int main()
C++ public Access Modifier {
class Sample // declare a class object
{ Sample obj1;
public: // public elements cout << "Enter your age: ";
int age; cin >> obj1.age;
void displayAge() obj1.displayAge();
{ return 0;
cout << "Age = " << age << endl; }
}
};
39
PRIVATE ACCESS MODIFIER
The private keyword is used to create private
members (data and functions).
The private members can only be accessed from
within the class.
However, friend classes and friend functions can
access private members.
SYNTAX:
private data_type variable_name
(Or)
private return_type method_name()
{
// data member
// member functions
40
};
Example 2: int main()
C++ private Access Specifier {
class Sample int ageInput;
{ Sample obj1; //Object Declr
private: //private elements
cout << "Enter your age: ";
int age;
cin >> ageInput;
private: // public method
obj1.displayAge(ageInput);
void displayAge(int a)
return 0;
{
}
age = a;
.
cout << "Age = " << age << endl;
} cin >> obj1.age; // error
In main(), the object obj1 cannot
};
directly access the
41
class variable age
PROTECTED ACCESS MODIFIER
Before we learn about the protected access specifier,
make sure you know about inheritance in C++.
The protected keyword is used to create protected
members (data and function).
The protected members can be accessed within the
class and from the derived class.
SYNTAX:
protected data_type variable_name
(Or)
protected return_type method_name()
{
// data member
// member functions
42
};
STATIC MEMBER DATA
&
STATIC MEMEBR FUNCTION
CLASS SPECIFICATION
SYNTAX:
Class Class_name
{
Data member
Member function
}
44
INTRODUCTION
Data Members:
The variables declared inside the class are known as data
members.
Data members may be private or public.
Member functions:
The functions declared inside the class are known as
member functions.
Member functions are methods or functions that are
defined inside of objects.
Generally used to manipulate data members and other
object data. 45
STATIC DATA MEMBERS
Define the data member of a class using the static
keyword, the data members are called the static data
member.
A static data member is similar to the static member
function because the static data can only be accessed
using the static data member or static member function.
All the objects of the class share the same copy of the
static member to access the static data.
46
DECLARE STATIC DATA
MEMBER
Syntax:
static data_type data_member_name;
Ex:
Class Demo
{
Static int count;
}
47
class rectangle
void output()
{ {
private: count++;
area=length*width;
int length; cout<<"area of rectangle"<<count<<"is=“
<<area<<endl;
int width;
}
int area; };
int rectangle::count;
static int count;
int main()
public: {
rectangle r1, r2,
void input()
r3; r1.input();
{ r1.output();
r2.input();
cout<<"enter the len"<<endl; r2.output();
cin>>length; r3.input();
r3.output();
cout<<"enter the width"<<endl; return 0;
cin>>width; }
}
48
STATIC MEMBER FUNCTIONS
Its special functions used to access the static data
members or other static member functions.
A member function is defined using the static
keyword.
A static member function shares the single copy of
the member function to any number of the class'
objects.
We can access the static member function using the
class name or class' objects.
49
class Member int Member :: A = 20;
{ int Member :: B = 30;
private:static int A,B,C; int Member :: C = 40;
Member() {
{ Member mb;
cout << " Print the static member
count++;
through object name: " << endl;
}
mb. disp();
static void disp() cout << " Print the static member
{ through the class name: " << endl;
} 50
};
DEFAULT ARGUMENTS
(PARAMETERS)
In C++ programming, we can provide default values
for function parameters.
}
C++ FUNCTIONS
A function is a group of statements that together
perform a specific task.
53
ADVANTAGE OF FUNCTION
Code Re-usability
54
FUNCTION ASPECTS
There are three aspects of a C function.
Function declaration
- A function must be declared globally.
Function call
-Function can be called from anywhere in the program.
-The parameter list must be same in function calling and
declaration.
Function definition
-The actual body of the function can be defined separately.
55
FUNCTION ASPECTS
56
TYPE OF FUNCTION
57
PRE-DEFINED FUNCTIONS
The pre-defined functions or library functions are
built-in functions.
Example: sqrt(),clrscr(),getch(),etc…,
58
USER-DEFINED FUNCTIONS
The functions defined by the user for their
requirement are called user-defined functions.
Example: sum(a,b)
59
ADVANTAGE OF USER-DEFINED
FUNCTIONS
Avoid rewriting same logic/code again and again.
multiple functions.
Code Reusability.
60
C++ OVERLOADING
If we create two or more members having the same
name but different in number or type of parameter, it
is known as C++ overloading.
61
C++ FUNCTION OVERLOADING
Function Overloading is defined as the process of
having two or more function with the same name, but
different in parameters is known as function
overloading.
n function overloading, the function is redefined by
using either different types of arguments or a
different number of arguments.
62
FUNCTION OVERLOADING - NUMBER OF ARGUMENTS VARY.
#include <iostream> return a + b + c;
using namespace std;
class Cal
{
public:
static int add(int a,int b)
{
return a + b;
}
static int add(int a, int b, int
c)
{
} 63
};
int main(void)
{
Cal C;
cout<<C.add(10,
20)<<endl;
cout<<C.add(12, 20,
23);
return 0;
}
FUNCTION OVERLOADING -DIFFERENT TYPES OF ARGUMENTS
65
Friend function used to print the length of
the box
#include <iostream> int printLength (Box b)
Using namespace {
b. length =10;
std; class Box return b.
{ length;
}
private: int main ()
{
int length;
Box b;
public: cout <<"Length of box:"<<printLength(b);
return 0;
}
friend int printLength (Box);
};
66
CONST
&
VOLATILE FUNCTIONS
67
CONSTANT FUNCTIONS
Member function does not alter any data in the class.
SYNTAX:
<return type> <function name> (argument1,argument 2)const
{
<the function body>
}
68
#include <iostream> int main()
using namespace {
std; class sample sample s;
{ s.disp();
public: int a=10; return 0
void disp()const }
{
cout<<a++;
ERROR DISPLAYED
} CAN NOT MODIFY A CONST OBJECT
};
69
MUTABLE DATA MEMBERS
Data members cannot be modified by the const
function.
If a data member is declared as mutable the it can be
even modified by const function.
SYNTAX:
mutable data_type variable_name;
EX:
mutable int a;
70
#include <iostream> };
using namespace int main()
std; class sample {
{ sample s;
public: s.a=10;
mutable int a=10; s.disp();
void disp()const return 0
{ }
cout<<a++; 10
}
71
VOLATILE FUNCTIONS
A member function can be declared as volatile if it is
invoked by volatile object.
72
class B cout<<z;
{ }
volatile int };
x=10; const int int main()
y=5; public: {
volatile void f() B b;
{ b.f();
cout<<x<<endl; return 0;
cout<<y<<endl; }
x++;
cout<<x<<endl;
z++; 73
CONSTANT OBJECTS
SYNTAX:
const class_name obj_name;
74
#include <iostream>
using namespace std;
class obj
{
public:
int a=10;
};
int main ()
{
const obj b;
cout << " The value of A: " <<b.a << endl;
// b.a = 20; // It returns a compile time error
return 0; 75
}
POINTER TO OBJECT IN C++
SYNTAX:
classname *pointertoobject;
76
#include <iostream> int getArea()
using namespace std; {
class Rectangle return 2*length*breadth;
{ }
private: };
int length; int main()
int breadth; {
public: Rectangle var1(10,30);
Rectangle(int l, int b) Rectangle* ptr = &var1;
{ int area = ptr->getArea();
length=l; cout<<"Area of Rect is:”
breadth=b; <<area;
return 0;
} 77
}
LOCAL CLASSES IN C++
81
82