Pages

Tuesday 31 August 2021

Linear Search Program in c++

Linear Search Program without class  

#include<iostream.h>
#include<conio.h>
void main()
{
 int a[10],i,n,x,flag=0;
 clrscr();
 cout<<"Enter the size"<<endl;
 cin>>n;
 cout<<"Enter the elements"<<endl;
 for(i=0;i<n;i++)
cin>>a[i];
 cout<<"Enter the element to search"<<endl;
 cin>>x;
 for(i=0;i<n;i++)
 {
if(a[i]==x)
{
flag=1;
break;
}
 }
 if(flag)
cout<<"\n Element "<<x<<" is found at position "<<i+1;
 else
cout<<"\n Element "<<x<<" not found";
getch();
}

Linear Search Program with class 

#include<iostream.h>
#include<conio.h>
class lsearch
{
 public:
int a[10],i,n,x,flag;
 public:
void getdata();
void search();
};
void lsearch::getdata()
{
 cout<<"Enter the size"<<endl;
 cin>>n;
 cout<<"Enter the elements"<<endl;
 for(i=0;i<n;i++)
cin>>a[i];
}
void lsearch::search()
{
 cout<<"Enter the element to be searched"<<endl;
 cin>>x;
 flag=0;
 for(i=0;i<n;i++)
 {
if(a[i]==x)
{
flag=1;
break;
}
 }
 if(flag)
cout<<"Element "<<x<< "found at position "<<i+1;
 else
cout<<"Element "<<x<<"not found";
}
void main()
{
lsearch lse;
clrscr();
lse.getdata();
lse.search();
getch();
}


Linear search Program with Address  --- Array  --- Data
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class linearsearch
{
 public:
int a[10],i,n,item,flag;
 public:
void getdata();
void search();
};
void linearsearch::getdata()
{
 cout<<"Enter the size"<<endl;
 cin>>n;
 cout<<"Enter the elements"<<endl;
 for(i=0;i<n;i++)
 {
  cout<<"Element a["<<i<<"] :"<<ends;
  cin>>a[i];
  }
cout<<"\nAddress"<<setw(25)<<"Array"<<setw(12)<<"Data"<<endl;
for(i=0;i<n;i++)
 {
  cout<<"\nAddress:"<< &a[i] <<"\tArray:"<<"a["<<i<<"]"<<"\tData:"<<a[i];
  cout<<"\n";
 }
}
void linearsearch::search()
{
 cout<<"Enter the element to be searched"<<endl;
 cin>>item;
 flag=0;
 for(i=0;i<n;i++)
 {
if(a[i]==item)
{
cout<<"Element "<<item<< "found at position "<<i+1;
flag=1;
break;
}
 }
 if(flag==0)
cout<<"Element "<<item<<"not found in the array";
}
void main()
{
linearsearch ls;
clrscr();
ls.getdata();
ls.search();
getch();
}

Output:

Linear search animation video link
https://www.youtube.com/watch?v=PDE8pCQ9Tz4

Friday 27 August 2021

BASIC CONCEPTS OF OOP’S

 BASIC CONCEPTS OF OOP’S

  • CLASS
  • OBJECT
  • DATA ABSTRACTION
  • DATA HIDING
  • ENCAPSULATION
  • INHERITANCE
  • POLYMORPHISM
  • DYNAMIC BINDING
  • MESSAGE COMMUNICATION 

CLASSES AND OBJECTS

Class: class is a user defined data type that represents an entity, which is a collection of members and methods.

The entire set of data and code of an object can be made the user define data type with help of a class. Infact objects are variables of type class. Once a class has been defined, we can create any number of objects belonging to that class.

Syntax:  

class <class name>
          {
            access specifier/visibility mode:
            variable declarations/data members;
            access specifier/visibility mode:
            function declarations/member functions;
         };
 

The keyword class specifies that what follows is abstract data of type class. The class body contains the declarations of variables and functions, which are called as class members. The variables declare inside the class are known as data members and functions are known as member functions.

C++ supports three types’ access specifiers

a) public

b) private

c) protect

 These three keywords are also known as visibility modes.

Public: It indicates the accessibility of a member to the outside class i.e., the data members can be accessed by any function from the outside. These members are used to provide an interface for the outside classes.

Private: The members can be accessed only by the member functions. The class members that have been declared as private can accessed only from within the class. By default, the members of class are private. Data hiding is possible with the help of private keyword.

Protected: The member data can be accessed by member functions of that class. The member is also accessible to member functions and the friend functions that are derived from this class. But they are not available to any one of the outside function. This specifier is most recently in the concept of Inheritance. 

DIFFERENCES BETWEEN THE THREE ACCESS SPECIFIERS 

Public

Private

Protected

Accessible from own class members

Accessible from own class members

Accessible from own class members

Accessible from derived class members

Not Accessible from derived class members

Accessible from derived class members

Accessible from objects outside the class

Not Accessible from objects outside the class

Not Accessible from objects outside the class


The declaration of an object is similar to that of a variable of any basic type.
Syntax: classname obj_name;
Example: student s;
where s is an object of class student.
 
Class: is a collection of data & functions. Class is an entity, which may represent a person, place, and location anything which we can feel it in real-life.
 
Object: is a functionable element that is used to access the members and methods of a class. Simply, object is an instance of class.
 
Example Program on Class and Object
 
#include<iostream.h>
#include<conio.h>
class examp
{
int a,b,t;
public:
void getdata();
void swap();
void disp();
};
void examp::getdata()
{
cout<<"enter a and b values"<<endl;
cin>>a>>b;
}
void examp::swap()
{
t=a;
a=b;
b=t;
}
void examp::disp()
{
cout<<"After swapping";
cout<<a<<"\t"<<b;
}
void main()
{
examp e;
clrscr();
e.getdata();
e.swap();
e.disp();
getch();
}
Output:
enter a and b values
2 3
After swapping 3 2

DATA ABSTRACTION

Abstraction refers to an act of representing only external features without including the background details or explanations. The process of extracting the essential features without exploring non-essential things is part of object. Abstraction permits the programmer to look at something without being concerned with its internal details

Example


Driver is an implementer here. If he was about to stop he has to stop he is not responsible for the rest of the non-essential things. Creating new data types using encapsulated data item, which suits to an application to be programmed is known as data abstraction.

DATA HIDING AND ENCAPSULATION

Data hiding is an insulation of the data from the direct access of the program is called as Data Hiding. It is a process of attaching the data closely to the system by protecting the data from other parts of the system. It means that data is concealed within a class, so that it cannot be accessed by functions outside the class even by mistake. The mechanism used to hide data is to put it in a class and make it private.

Encapsulation it is a method of wrapping up of data and functions into a single unit. This is the central idea of OOP. The data cannot be accessed by the outside functions but the data can only be accessed by only those functions, which are wrapped in that single unit.



/* DATA HIDING AND ENCAPSULATION */

#include<iostream.h>
#include<conio.h>
class hiding
{
private:
     int age; /* data hiding */
public:
     void setData(int);
     void print();
};
void hiding::setData(int x)
{
if(age>0)
age=x;
}
void hiding::print()
{
cout<<age<<endl;
}
void main()
{
clrscr();
hiding h1;
h1.setData(13);
h1.print();
getch();
}
Output:
13

 INHERITANCE

The mechanism of deriving a new class from an old one is called as Inheritance. The old class is called as base class and the new one is called as derived class.  The Derived class inherits some or all the features of the base class. A class can also inherit properties from more than one class or from more than one class.


Example : 
Single inheritance Program using the visibility mode of public

#include<iostream.h>
#include<conio.h>
class stdbaseclass
{
 private:
 int sno;
 char sname[22];
 public:
void getstd()
{
cout<<"enter sno and sname";
cin>>sno>>sname;
}
void putstd()
{
cout<<"std  no is" <<sno<<endl;
cout<<"std name is" <<sname<<endl;
}
};

class phyderivedclass:public stdbaseclass
{
 int h,w;
 public:
void getphy()
{
cout<<"enter height and weight";
cin>>h>>w;
}
void putphy()
{
cout<<"std  height is" <<h<<endl;
cout<<"std weight is" <<w<<endl;
}
};

void main()
{
 phyderivedclass p;
 clrscr();
 p.getstd();
 p.putstd();
 p.getphy();
 p.putphy();
 getch();
}

Example : 
Multiple inheritance Program using the visibility mode of private 
#include<iostream.h>
#include<conio.h>
class stdbc
{
 private:
 int sno;
 char sname[22];
 public:
void getstd()
{
cout<<"enter sno and sname";
cin>>sno>>sname;
}
void putstd()
{
cout<<"std  no is" <<sno<<endl;
cout<<"std name is" <<sname<<endl;
}
};

class markbc
{
 protected:
 int m1,m2,m3;
 public:
void getmarks()
{
cout<<"enter m1 m2 m3 marks ";
cin>>m1;
cin>>m2;
cin>>m3;
}
void putmarks()
{
cout<<"marks m1 is" <<m1<<endl;
cout<<"marks m2 is" <<m2<<endl;
cout<<"marks m3 is" <<m3<<endl;
}
};

class resultdc : public stdbc ,public markbc
{
 int tot;
 float avg;
 public:
void show()
{
tot = m1+ m2+ m3;
cout<<"total marks is ";
cout<<tot<<endl;
avg=tot/3.0;
cout<<"average marks is ";
cout<<avg<<endl;
}
};

void main()
{
 clrscr();
 resultdc r;
 r.getstd();
 r.getmarks();
 r.putstd();
 r.putmarks();
 r.show();
 getch();
}

Program on Access specifier 
(public,private,protected) 

 

WITHIN

CLASS

OUTSIDE

CLASS

DERIVED

CLASS

PUBLIC

YES

YES

YES

PRIVATE

YES

NO

NO

PROTECTED

YES

NO

YES


Program on Access specifier (public)
#include<iostream.h>
#include<conio.h>
class accspe
{
 public:
 int x;
 private:
 int y;
 protected:
 int z;
};

void main()
{
 clrscr();
 accspe as;
 cout<<"enter x public value:"<<endl;
 cin>>as.x;
 cout<<"x public value:"<<as.x<<endl;
 getch();
}

Program on Access specifier (private)
#include<iostream.h>
#include<conio.h>
class accspe
{
 public:
 int x;
 private:
 int y;
 protected:
 int z;
 public:
 void display()
 {
   y=20;
   cout<< "y value can be seen if 
               it is inside the class private:"<<y<<endl;
 }
};

void main()
{
 clrscr();
 accspe as;
 cout<<"enter x public value:"<<endl;
 cin>>as.x;
 cout<<"x public value:"<<as.x<<endl;
/* cout<<"enter y private value:"<<endl;
 cin>>as.y;
 cout<<"y private value:"<<as.y<<endl;
 */         we will get error if we remove comment
 as.display();
 getch();
}

Program on Access specifier (protected)
#include<iostream.h>
#include<conio.h>
class accspe
{
 public:
 int x;
 private:
 int y;
 protected:
 int z;
 public:
 void display()
 {
   y=20;
   cout<< "y value can be seen if it is inside the class private:"<<y<<endl;
 }
};

class derived : public accspe
{
  public:
  void show()
  {
  z=30;
  cout<< "z value Potected is "<<z<<endl;
  }
};

void main()
{
 clrscr();
 accspe as;
 cout<<"enter x public value:"<<endl;
 cin>>as.x;
 cout<<"x public value:"<<as.x<<endl;
/* cout<<"enter y private value:"<<endl;
 cin>>as.y;
 cout<<"y private value:"<<as.y<<endl;
 */
 as.display();
 derived d;
 d.show();
 getch();
}


Polymorphism:
        poly means many
        morphism means form

Example:

I.Static /compile/early binding
  1. Function Overloading: 2 or more functions can have same name with different parameters
II.Dynamic/Runtime/late binding
  1. Function overriding:2 or more functions can have same name with same parameters
  2. Inheritance should be there if you are using function overriding. It can not be done with in a class for this we required derived class and base class

Program on Function Overloading:

// Function overloading Program using c++
#include<iostream.h>
#include<conio.h>
class addition
{
 public:
 void sum(int a,int b)
 {
 cout<<"a+b :"<< a+b;
 }
 void sum(int a,int b,int c)
 {
 cout<<"a+b+c :"<< a+b+c;
 }
};

void main()
{
 clrscr();
 addition a;
 a.sum(11,22);
 a.sum(1,2,3);
 getch();
}

Program on Function Overriding:

// Function overriding Program using c++
#include<iostream.h>
#include<conio.h>
class base
{
 public:
 void show()
 {
 cout<<"Base class"<<endl;
 }
};

class derived : public base
 {
 public:
  void show()
  {
  cout<<"derived class"<<endl;
  }
};

void main()
{
 clrscr();
 base b;
 derived d;
 b.show();
 d.show();
 getch();
}


INTRODUCTION TO OBJECT ORIENTATION AND C++

C++ is an Object Oriented Programming Languages (OOPL). It was developed by Mr.Bjarne Stroustrup in 1980 at Bell Laboratories. It is an extension of C-Language. It was initially named as ‘C with Classes’.

NEED FOR OOP APPROACH:

Object Oriented Programming is an approach to program organization and development that attempts to eliminate the pitfalls of Procedure Oriented Programming

With advent of languages such as C became very popular and it was the main technic 0f 1980’s.

Structured Programming was a popular tool for developing moderately complex programs

As the size and complexity of program increase the structured programming approach failed in developing big tree programs that are easy to maintain and reusable.

Once the program exceeds 20,000 lines of code it become very difficult to group the essence of the program in totality.

Out of the total cost of software around 90% is spent on debugging and maintenance

Only 2% of software objects undertaken by U.S. Defence Department in 1970’s worked successfully.

This increasing complexity of programs as necessitated for the development of new programming approach of OOP. It is such an approach that eliminates the drawback of Structured Programming Approach.

MAIN ADVANTAGE OF OOP OVER SOP

In SOP, most of the data is created as global; hence all the functions in the program can access the data. If there are 100 of functions if any function mismanipulates the data by mistake it will be very difficulty to check the code of all these functions to find out the error. As the complexity of the program increases it will become almost impossible to find out the bugs.

Procedure Oriented Approach

In OOP, most of the data will be private only a few functions, can manipulate the data. The data and functions that operate on the data will be encapsulated as a single unit and such a unit is called as Object. If any outside function want to access the data it has to make use of the few functions, which can access the data i.e., the outside functions cannot directly access the data. In this approach, if data is mismanipulated by mistake we need to check the code of only a few functions. Therefore, the debugging process is considerably reduced.

Object Oriented Approach

 

DIFFERENCES BETWEEN POP AND OOP

 

Procedure Oriented Programming

Object Oriented Programming

Emphasis is on doing things

Emphasis is on data rather than procedures

Programs are divided into small things known as functions

Programs are divided into what are known as Objects

Data move openly around the system from function to function

Data is hidden and cannot be accessed by external functions

Debugging Process is very difficult

Debugging Process is very easy

Follows top-down approach in program design

Follows bottom-up approach in program design

Most of the functions share global data

The data of an object can be accessed only by the functions that are associated with it

 

Reference Variable:  It Provides an alias for previously defined variable.

Ex:- int total=500;

     Int &sum=total;

Syn: datatype &ref_name=var_name;

Both variables refer to the same data in the memory. If a change made to 1 variable will effect another variable.

Operators in C++:

>>->Extraction

<<->Insertion

:: ->scope resolution

.

:->Inheritance Operator

&->Reference Operator

new

delete

->*àPointer to member

 

INTRODUCTION TO C++

        C++ is an OOP language developed by Bjarne Stroustrup of Denmark at AT & T Bell Laboratories in New Jersey, USA in 1982.Stroustrup, an admirer of simula67 & a strong supporter of C wanted to combine the best of both languages & create a more powerful language that support Object – Oriented Programming features.

 DIFFERENCES BETWEEN C AND C++ 

C

C++

In C, main function return a value of void type

In C++, it returns a value of integer type

Prototyping for functions and procedures are optional

It is an strictly typed language which makes compulsory for specifying prototype and procedures

C doesn’t give importance to data

It uses the concept of data hiding which restricts data to move away from the entity definition

C doesn’t support declaration of variables inline

It allows inline declaration wherever the user wants

Extension of source file is .c

Extension of source file is .cpp

The basic building block is a function

The basic building block is an object

C does not support future extension

It supports the concept of Inheritance through which new features can be added without redeclaring the existing one

C doesn’t support multiple definition for the same name which is basically overloading

C++ supports overloading both for a function and operators.


FEATURES OF OBJECT ORIENTED LANGUAGE
  • Emphasis is on data rather than procedure.
  • Programs are divided into what are known as objects.
  • Data structures are designed such that they characterize the objects.
  • Functions that operate on the data of an object are tied together in the data structure.
  • Data is hidden and cannot be accessed by external functions.
  • Objects may communicate with each other through functions.
  • New data and functions can be easily added whenever necessary.
  • Follows bottom-up approach in program design.

 BENEFITS OF OBJECT ORIENTED PROGRAMMING (OOP) 

  • The concept of a data class makes it possible to define subclasses of data objects that share some or all of the main class characteristics. Called inheritance, this property of OOP forces a more thorough data analysis, reduces development time, and ensures more accurate coding.
  • Since a class defines only the data it needs to be concerned with, when an instance of that class (an object) is run, the code will not be able to accidentally access other program data. This characteristic of data hiding provides greater system security and avoids unintended data corruption.
  • The definition of a class is reusable not only by the program for which it is initially created but also by other object-oriented programs (and, for this reason, can be more easily distributed for use in networks).
  • The concept of data classes allows a programmer to create any new data type that is not already defined in the language itself.
  • We can build programs from the standard working modules that communicate with one another, rather than having to start writing the code from scratch. This leads to saving of development time and higher productivity.
  • It is easy to partition the work in a project based on objects.
  • Object-Oriented systems can be easily upgraded from small to large systems.
  • Message passing techniques for communication between objects makes the interface descriptions with external systems much simpler.
  • The principle of data hiding helps the programmer to build secure programs that cannot be invaded by code in other parts of the program.
  • Software complexity can be easily managed.

 APPLICATIONS OF OOPS

  • Real – time Systems
  • Simulation and modeling
  • Object – Oriented databases
  • Hypertext, hypermedia and expertext
  • Artificial Intelligence
  • Neural works
  • CIM/CAM/CAD systems
  • Office automation systems
  • Decision support
  • Parallel Programming

Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...