0% found this document useful (0 votes)
114 views76 pages

C Notes1

kytggc

Uploaded by

namkumg16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views76 pages

C Notes1

kytggc

Uploaded by

namkumg16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 76

Infeepedia

Infeepedia

Join us at:
Facebook group:- Computer Science by Infee Ma’am
Telegram group:- https://https://t.me/+XwZXHJSEBtk0NWVl
Telegram channel:- Infeepedia
Whatsapp channel:- Infeepedia
Instagram:- https://www.instagram.com/infeepedia
Twitter:- https://twitter.com/infeepedia

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Procedural oriented programming Vs Object Oriented Programming
SNo. POPS OOPS
1. Based on functions, Based on real world objects.
procedures or subroutines

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
2. Complex to implement Easy to implement large
large applications. projects.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
3. It is less secure Highly secure

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
4. Overloading is not allowed. Overloading is allowed

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
5. Top down approach Bottom up approach

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Object oriented programming concepts

 Class
 Objects
 Inheritance
 Encapsulation
 Abstraction
 Polymorphism

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Class
 A user-defined derived data type or prototype or blueprint from
which objects are created.
 A logical entity that contains data members and member
functions.
 Data members are the variables that define the state of an
object.
 Member functions are the methods that define the behaviour
of an object.
 Memory is not allocated to classes. Classes have no physical
existence.
Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
 Class is a logical entity however an object is physical entity.
 Classes cannot be manipulated as there is no memory allocated.
But objects can be.
 Classes use access specifier to control the access of data
members and methods.
 If access specifiers is not mentioned then by default class
members are private.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
 Member functions can be defined in 2 types:
 Inside class definition:- member function defined with in the
class.
 Outside class definition:- To define a member function outside
the class definition we have to use the scope resolution(::)
operator along with the class name and function name.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Output:-

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Objects
 Object is a real world entity for example, chair, car, pen,
mobile, laptop etc.
 Objects are created to use the data
and methods of the class.
 To access data and member function
of class we can declare objects.
 Data defines the states and methods
define the behaviour of objects.
 Memory is allocated to an object.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
 The size of the object is dependent on the class data
members.
 We can create more than one object for a class and also can
create array type objects.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Constructor
 Constructors are special member functions that are used to
initialize objects of a class.
 It has the same name as the class and do not have a return
type.
 Constructors are automatically invoked when an object is
created.
 As the name suggest it constructs the values (i.e. provides data
for the object).
 Constructors can be overloaded.
 A constructor cannot be declared virtual.
Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
 A constructor cannot be inherited.
 The addresses of the constructor cannot be referred to.
 It must be placed in public section of class.
 The constructor can be defined inside the class declaration or
outside the class declaration.
Syntax of Constructors:
<class-name> (list-of-parameters);

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Syntax for Defining the Constructor Within the Class
<class-name> (list-of-parameters)
{
// constructor definition
}
Syntax for Defining the Constructor Outside the Class
<class-name>::<class-name>(list-of-parameters)
{
// constructor definition
}

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Ex1:-

Output:-

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Ex2:-

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
output:-

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Ex3:-

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Output:-

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Types of Constructor
Based on the situations they are being used constructor is
classified in 4 types.
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Move Constructor

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
1. Default constructor
A default constructor is a constructor that doesn’t take any
argument. It has no parameters. It is also called a zero-
argument constructor.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
2. Parameterized Constructors
 Parameterized Constructors is a constructor with arguments.
 These arguments help initialize an object when it is created.
To create a PC we add parameters to it like any other
function.
 When we define the constructor’s body, use the parameters
to initialize the object.
 When an object is declared in a parameterized constructor,
the initial values have to be passed as arguments to the
constructor function.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
 The normal way of object declaration may not work.
 The parameterized constructors can be called explicitly or
implicitly:
car c = car(“Swift”,”red” 500); // Explicit call
car c(“Swift”,”red” 500); // Implicit call
 When the parameterized constructor is defined and no
default constructor is defined explicitly, the compiler will not
implicitly create the default constructor and hence creating a
simple object as (car c) will give an error.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
 Whenever we define one or more non-default constructors(
with parameters ) for a class, a default constructor( without
parameters ) should also be explicitly defined as the compiler
will not provide a default constructor in this case.
 It is used to initialize the various data elements of different
objects with different values when they are created.
 It is used to overload constructors.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
3. Copy constructor
 It is used to copy one object’s value to another object of the
same class.
 A copy constructor is used to initialize an object using another
object of the same class.
 Syntax for explicit copy constructor:
It takes a reference to an object of the same class as an argument.
ClassName (ClassName &obj)
{
// body_containing_logic
}
Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
 If we don't define a copy constructor explicitly, the compiler
creates one implicitly. Even if we define other constructors,
the compiler will always create an implicit copy constructor
unless we specifically provide one.
 This behaviour is unlike the default constructor, where if you
define any constructor explicitly, the default constructor is
deleted.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Use of copy constructor:-
Constructs a new object by copying values from an existing
object.
Can be used to perform deep copy.
Modify specific attributes during the copy process if needed.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Destructor
 A destructor is a special member function used to destroy the
objects created by constructor. It cleans up memory and
release resources held by objects.
 A destructor has the same name as the class, but it is
preceded by a tilde (~) symbol.
 Only one destructor is allowed per class.
 Destructors don't take any arguments and don't return any
value therefore it cannot be overloaded
 Destructor cannot be declared as static and const;

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
 Destructor should be declared in the public section of the
program.
 When an object goes out of scope or is explicitly deleted, its
destructor is automatically called by the compiler.
 Destructors are executed in the reverse order of the
constructors. This ensures that resources are released in the
opposite order of their allocation.
 Like constructors, destructors can also be defined either
inside or outside of the class.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
The syntax for defining the destructor within the class
~ <class-name>(){}

The syntax for defining the destructor outside the class


<class-name>: : ~<class-name>() {}

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Access Specifiers
 To implement Data Hiding which is the most important
concept of the oops, we use access specifiers or modifier.
 Access Specifiers in a class decide the accessibility of the
class members(data and function) in other classes.
 Specifiers decide whether the class members will get directly
accessed by the blocks present outside the class or not,
 By default the access specifier for the class members will
be Private.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
There are three types of access modifiers in OOPS:
1. Public
2. Private
3. Protected

1. Public: If any class member (data members/member


functions) is declared as public then:–
1. They are accessible to everyone.
2. They can be accessed using the direct member access
operator (.).

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Output:-

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
2. Private: If any class member is declared as private then:
 It can only be accessed by functions declared inside the class
 It is not accessible from the outside of class
 Only the member functions or the friend functions/classes are
allowed to access the private data members of a class.
 If no specifier is mentioned then by default the class members
are private.
 We can access the private data members of a class indirectly
 By creating getter/setter functions
 By initializing values via constructors
 By setting values via public function
Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Output:-

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia

Output:-

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Output:-

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
3. Protected: If any class members are declared as protected
then:-
 It is the same as private accessibility.But, derived
(subclasses/child) classes can also access the protected class
members.
 The main difference between private and protected is that
protected inheritance allows continued access to base class
members whereas private inheritance prohibits the access of
those members.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
 These protected members are inaccessible outside. But, are
accessible in the derived class as protected data members.

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.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia

Output:-

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Specifier Within Same Class In Derived Class Outside the Class
Private Yes No No
Protected Yes Yes No
Public Yes Yes Yes

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Encapsulation
 Encapsulation is like putting something in a box and giving it
a lock.
 The purpose is to prevent access to the data
directly.
 Encapsulation means bundling the data
(variables) and the methods (functions for manipulating the
data) together into a single entity called a class.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
 This way, the data is protected from outside interference
and can only be accessed or modified through specific
methods.
 It's like having a locker where only certain people have the
key to access or modify what's inside.
 This helps to keep the code organized, secure, and easier to
manage.
 Encapsulation is used for data protection and data hiding.
 Encapsulation can be implemented with the help of access
specifiers(public, private and protected).

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
 Encapsulation improves readability, maintainability, and
security by grouping data and methods together.
 It helps to control the modification of our data members.
 We can create read only and write only modes by using
encapsulation.
 When we only get the data but cannot modify it is called
read only mode.
 When we only set the data it is called write only mode.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Example:-
#include<iostream> void getdata()
using namespace std; { cout<<"Name is: " <<name<<endl;
class car { cout<<"m_number is: "<<m_number<<endl;
private: } };
string name; int main()
int m_number; {
public: car c;
string color; c.color= “red”;
void setdata(string s1,int m1) c.setdata("Alto", 102);
{ c.getdata();
name = s1; cout<<"Color is: " <<color<<endl;
m_number = m1; return 0;
} }
Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Types of Encapsulation
We can implement encapsulation in three ways:
1. Member variable encapsulation
2. Function encapsulation
3. Class encapsulation

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
1. Data Encapsulation
#include<iostream> void getdata() {
using namespace std; cout << "Name is: "<<name<<endl;
class car { cout << "color is: "<<color<<endl;
private: cout << "m_number is: "<< m_number<<endl;
string name; }
string color; };
int m_number; int main() {
public: car c;
void setdata(string s1, string c1, int m1) c.setdata("Alto", "Red", 102);
{ c.getdata();
name=s1; return 0;
color=c1; }
m_number =m1; }
Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
2. Function Encapsulation
using namespace std; public:
class car { string name;
private: int m_number;
void speedcalculate() { int year;
int speed; void getspeed() {
if(m_number>200 && year>2020) { m_number=300;
speed= 150; year=2021;
cout<< "speed is "<< speed<<endl; speedcalculate(); } };
} else int main() {
{ speed=100; car c;
cout<< "speed is "<< speed<<endl; } c.getspeed();
} return 0; }
Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
3. Class Encapsulation
#include<iostream> cout<< "Address is "<<street <<" "<< city <<" "
using namespace std; << state <<" "<< zipcode << endl;
class student { return 0; }
private: };
class Address { Address a;
private: public:
string street, city, state, zipcode; void myaddress() {
public: a.getaddress();
string getaddress() }
{ };
street = "123 street"; int main() {
city = "Kanpur"; student s;
state = "UP"; s.myaddress();
zipcode = "211210"; return 0;
}

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
#include<iostream>
using namespace std; void getdata() {
class car { cout << "Name is: "<<name<<endl;
private: cout << "color is: "<<color<<endl;
string name; cout << "m_number is: "<< m_number <<
string color; endl;
int m_number; }
public: };
void setdata(string s1, string c1, int main() {
int m1) car c;
{ name=s1; c.setdata("Alto", "Red", 102);
color=c1; c.getdata();
m_number =m1; } return 0;}
Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Inheritance
In terms of programming it is a capability of class (child) to derive
data and behaviour of another class (parent) which increases
code reusability and reduce code size.
The class whose properties are acquired is called super
parent/base class.
The class which acquire all the properties is called derived
/sub/child class.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Need of inheritance
Class Vehicle
Name, color,model_number
Start(), stop(), applybrake()

Class erikshaw
Name, color,model_number
Class bike Start(), stop(), applybrake()
Name, color,model_number Charge()
Start(), stop(), applybrake()
Halmetholder() Class car
Name, color,model_number
Start(), stop(), applybrake()
Mode ofReverse()
inheritance
Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Mode of inheritance
There are three modes of inheritance:

Base class Mode Inheritance mode


Of access specifiers Public Protected Private
Public Public Protected Private
Protected Protected Protected Private
Private Not accessible Not accessible Not accessible

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Types of Inheritance
There are five types of inheritance:
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
1. Single Inheritance
When the derived class inherits the properties of only one base class, it
is called as Single Inheritance.
A(base class)

B(Derived class)

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
#include<iostream> class car: public vehicle
using namespace std; {
class vehicle public:
{ int wheels;
public: void reverse()
string name; {
void start() cout<< "car is in back gear. "<< endl;
{ } };
cout<< "vehicle started "<< endl; int main()
cout<< "name is " << name<< endl; {
} car c;
void stop() c.name="WagonR";
{ c.start();
cout<< "vehicle stopped " << endl; c.stop();
} }; c.reverse(); }

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Output:-
vehicle started
name is WagonR
vehicle stopped
car is in back gear.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Multiple Inheritance
When a derived /child/sub class inherits the properties of more than one
base/parent/super class, it is called multiple inheritance.

A(base class) B(base class)

C(Derived class)

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
#include<iostream> class mechanical
using namespace std; {
class Electronics { public:
public: string weight;
string range; void pushed()
void on() {
{ cout<< " push button is
cout<< "on button is pressed "<< endl; pressed "<< endl;
cout<< "access range is " << range<< endl; cout<< "weight is " <<
} weight<< endl;
}
void off()
};
{
cout<< "off button is pressed" << endl;
} };
Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
class car: public Electronics, public int main()
mechanical {
{ car c;
public: c.range="10 mtr";
int wheels; c.weight= "300kg";
void reverse() c.on();
{ c.pushed();
cout<< "car is in back gear. "<< endl; c.reverse();
} }
};
Output:- vehicle started
name is WagonR
vehicle stopped
car is in back gear.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Diamond Problem
 Suppose A’s properties are inherited by B and C classes and then D
inherits the properties of both B and C, as this situation is creating
a diamond shape, therefore, this problem
is known as “diamond problem”.
 In this situation classes B and C both can
access the properties of A.
 D can access the properties of B and C. So
D can access A via B and via C.
 This causes ambiguity. As the compiler will get confused.

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Multilevel Inheritance
When a derived class inherits the properties of the base class and
further it become the base class(parent class) to the other class (or
further it’s property is inherited by some other class), it is called
Multilevel Inheritance.
There can be any number of levels i.e any number of derived classes
in multilevel inheritance. A(base class)

B(Derived class/base class for C)

C(Derived class)

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
#include<iostream> class car: public vehicle
using namespace std; {
class vehicle public:
{ int wheels;
public: void reverse() {
string name; cout<< "car is in back gear. "<< endl;
void start() } };
{ class maruti_suzuki: public car
cout<< "vehicle started "<< endl; { public:
cout<< "name is " << name<< endl; int m_number;
} void print(int x)
void stop() { m_number =x;
{ cout<< "model number is "
cout<< "vehicle stopped " << endl; <<m_number<<endl;
} }; } };

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
int main() Output:-
{ on button is pressed
maruti_suzuki m; access range is 10 mtr
m.name="WagonR"; push button is pressed
m.start();
weight is 300kg
m.stop();
car is in back gear.
m.reverse();
m.print(193);

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Hierarchical Inheritance
When more than one derived class inherit the properties of single
base class, it is called Hierarchical Inheritance.

A(base class)

B(derived class) C(Derived class)

D(derived class) E(derived class)

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
#include<iostream> class car: public vehicle
using namespace std; { public:
class vehicle int wheels;
void reverse()
{
{ out<<"car is in back gear."<<
public: endl;
string name; } };
void start() class bike: public vehicle
{ { public:
cout<< name <<" is started "<< endl; string type;
} void halmetholder()
{cout<< "this is a "<< type<<
void stop()
"bike" <<endl;
{ cout<< "halmet holder is
cout<< name <<" is stopped "<< endl; opened"<<endl;
} }; } };
Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
int main() Output:-
{ WagonR is started
car c; pulser is stopped
bike b; car is in back gear.
c.name="WagonR";
b.name="pulser";
c.start();
b.stop();
c.reverse();
}

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Hybrid inheritance
It is a combination of one or more types of inheritance.

A(base class) A(base class)

B(derived class) C(Derived class)

D(derived class) E(derived class)

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
#include<iostream> class Country {
using namespace std; public:
class World { Country() {
public: cout << "We are in Country"<<endl;
World() { } };
cout << "we are in World!"<< endl; // multiple Inheritance.
} }; class India: public Continent, public Country {
// Single Inheritance. public:
class Continent: public World { India() {
public: cout << "We are in India!";
Continent() { } };
cout << "We are in Continent\n"; int main() {
} India I;
}; }

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia
Infeepedia
Output:-
we are in World!
We are in Continent
We are in Country
We are in India!

Join us at:- Computer science by infee ma’am Like share and subscribe Infeepedia

You might also like