Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

Object Oriented Programming

Unit-1
Array
•An array in C or C++ is a collection of items stored at contiguous memory locations and elements can be
accessed randomly using indices of an array.

•They are used to store similar type of elements as in the data type must be the same for all elements.

•They can be used to store collection of primitive data types such as int, float, double, char, etc of any
particular type.
Strings

• Array of Characters
• char a[20]={’a’,’b’,’c’,’d’,.......};
• char name[20]=”pratibha soram”;
• cout<<“pratibha”;
• char surname[20];
• cin>>surname;
• cout<<“soram”;
Sr.No Function & Purpose

1 strcpy(s1, s2);

Copies string s2 into string s1.

2 strcat(s1, s2);

Concatenates string s2 onto the end of string s1.

3 strlen(s1);

Returns the length of string s1.

4 strcmp(s1, s2);

Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.

5 strrev(s1);

Reverse the string s1.


Functions in C++
-A function is a set of statements that take inputs, do some specific
computation and produces output.
-The idea is to put some commonly or repeatedly done task together and make
a function so that instead of writing the same code again and again for different
inputs, we can call the function.
Syntax:
return_type function_name([ arg1_type arg1_name, ... ]){
//code
}
Defining a function
Function Components
• Function Prototype or declaration
• Function Call
1. Call by Value
2. Call by Reference
• Function Definition
1. No Argument, No return; void func();
2. No Argument, Return; int func();
3. Argument, No return; void func(int x);
4. Argument, return; int func(int x);
Types of Functions
• Iterative Functions
• for, while, and do-while
• for(int i = 0; i < 5000; i++) {
cout << ……………..;
}
• Recursive Functions

• a function actually calls itself


Features of OOP
1. ENCAPSULATION
• Class & objects
• Constructors & destructors
2. POLYMORPHISM
• Compile time polymophismv(constructer overloading, operator overloading, function overloading)
• Run time polymorphism (virtual functions, pure virtual functions)
3. INHERITANCE (single, multiple, multilevel and hybrid)
4. TEMPLATES (class templates, function templates)
Access specifiers
• Also known as access Modifiers
• Declared inside a class
• Used to assign the accessibility to the class members, i.e., they set some
restrictions on the class members so that they can’t be directly accessed by the
outside functions.
• There are 3 types of access modifiers available in C++:
• Public
• Private
• Protected

Note: If we do not specify any access modifiers for the members inside the class, then by
default the access modifier for the members will be Private.
Access specifiers (Contd.)
• Public
• All the class members declared under the public specifier will be available to everyone.
• The public members of a class can be accessed from anywhere in the program using the direct
member access operator (.) with the object of that class.
• Private
• The class members declared as private can be accessed only by the member functions inside the
class.
• They are not allowed to be accessed directly by any object or function outside the class.
• Only the member functions or the friend functions are allowed to access the private data members
of the class.
• Protected
• The protected access modifier is similar to the private access modifier in the sense that it can’t be
accessed outside of its class unless with the help of a friend class.
• The difference is that the class members declared as Protected can be accessed by any subclass
(derived class) of that class as well.
• Note: This access through inheritance can alter the access modifier of the elements of base class in
derived class depending on the mode of Inheritance.
Approaches in POP and OOP
POP (Procedural Oriented OOPs (Object Oriented
Programming) Programming)
main(){}
Class1;
subprog1(){} Class2;
Class3;
subprog2(){}
main(){}
C++
- Invented by Bjarne Stroustrup (currently a Professor of Computer
Science at Columbia University)
- Enhanced version of c (c=c+1)
- C was POP
- C++ OOP
C++ (Contd.)
•Header file- iostream
:: scope resolution operator
•class

•cin , handles the input stream


printf(“hello”); replaced by
• cout, handles the output stream
cout<<”hello”;
<< insertion operator used with cout

>> extraction operator used with cin scanf(“%s”,&name); replaced by


istream+ostream=iostream
cin>>name;
istream- input stream, ostream-output stream
Operators
• Arithmetic
• Logical
• Relational
• Conditional
• Special comma
• Dot
• ++, --
• <<,>>
• New
• delete
• scope resolution
• pointer
Conditional
• Syntax

(expression 1) ? expression 2 : expression 3

If expression 1 evaluates to true, then expression 2 is evaluated.

If expression 1 evaluates to false, then expression 3 is evaluated


instead.
Encapsulation
• Process of wrapping up of data and its code together to make it secure
from outside world. Capsule is called class. We can access the class by
its object.
• class book
{
int accno;
char title[20];
char author[100];
int price;
};
Syntax of class
class class_name
{
Data members;
member functions;
};
int main(){
//code
}
Function Overloading
scanf(“%d”,&r); • You can see that there are two
void area(r) functions with the same name(area) but
{ different arguments/parameters
float a; indicating a function overloading in the
a=3.14*r*r; program.
}
scanf(“%d%d”,&b,&h);
void area(b,h)
{
float a;
a=0.5*b*h;
}

stud3=stud1+stud2;
Inline Function
• to reduce the function call overhead.
• a function that is expanded in line when it is called.
• when the inline function is called whole code of the inline function gets
inserted or substituted at the point of the inline function call.
• substitution is performed by the C++ compiler at compile time.
• An inline function may increase efficiency if it is small.
• Syntax:
inline return-type function-name(parameters) {
// function code
}
Inline Function (Contd.)
• inlining is only a request to the compiler, #include <iostream>
not a command. using namespace std;
• The compiler can ignore the request for inline int cube(int s) {
inlining for the following circumstances: return s * s * s;
1. If a function contains a loop. (for, }
while and do-while) int main()
2. If a function contains static variables. {
3. If a function is recursive. cout << "The cube of 4 is: " << cube(4);
4. If a function return type is other than return 0;
void, and the return statement doesn’t }
exist in a function body.
5. If a function contains a switch or goto • Output:
statement. The cube of 4 is: 64
Scope resolution operator
1. To access a global variable when there is a local variable with same name

#include<iostream>
using namespace std;
int a; // Global a
int main() {
int a = 23; // Local a
cout << "Value of global x is " << ::x;
cout << "\n Value of local x is " << x;
return 0; Output: Value of global a is 0
} Value of local a is 23
Scope resolution operator (Contd.)
2. To define a function outside a class
#include <iostream> int main()
using namespace std; {
class A { A a;
public: a.fun();
void fun(); // Only declaration return 0;
}; }
void A::fun() { // Definition outside class using ::
cout << "function func() is called"; Output:
} function func() is called
Scope resolution operator (Contd.)
3. To access a class’s static variables. // In C++, static members must be explicitly
defined like this, outside the class
#include<iostream>
int S::x = 1;
using namespace std;
int S::y = 2;
class S{ int main(){
static int x; S s1;
public: int x = 3 ;
static int y; // Local parameter 'x' hides class member 'x', but we can s1.func(x);
access it using ::
cout << "\n S::y = " << S::y;
void func(int x) { return 0;
// We can access class's static variable even if there is a local variable }
cout << "Value of static x is " << S::x; Output:
cout << "\n Value of local x is " << x; Value of static x is 1
} Value of local x is 3
}; S::y = 2
Passing objects as arguments
#include <bits/stdc++.h> cout << "Initial Values \n";
//header file that we use in our code to include all the standard libraries cout << "Value of object 1: " << E1.a
using namespace std; << "\n and object 2: " << E2.a
class Error{ << "\n\n";
public: E2.add(E1); // Passing object as an argument to
function add()
int a;
// Display changed values after passing object as
void add(Error E) { // This function will take an object as an argument argument
a = a + E.a; cout << "New values \n";
} cout << "Value of object 1: " << E1.a
}; << "\n and object 2: " << E2.a
int main(){ << "\n\n";
Error E1, E2; // Create objects return 0;
E1.a = 7; // Values are initialized for both objects }
E2.a = 20;

You might also like