Oop DS U1
Oop DS U1
Concepts of OOPS 1
OOP refers to Object-Oriented Programming, and it deals with objects and their properties. The
major concepts of OOP are classes/objects, abstraction, encapsulation, polymorphism, and
inheritance.
POP refers to Procedural-Oriented Programming and deals with programs and functions. In POP,
programs are divided into functions, and data is global.
OOP stands for Object Oriented POP stands for Procedural Oriented
1 Definition
Programming. Programming.
OOP follows a bottom up
2 Approach POP follows a top down approach.
approach.
A program is divided into
3 Division A program is divided into objects.
functions.
Inheritance
4 Inheritance is supported. Inheritance is not supported.
supported
Access control is supported via
5 Access control No access modifiers are supported.
access modifiers.
No data hiding present. Data is
6 Data Hiding Encapsulation is used to hide data.
globally accessible.
7 Example C++, Java C, Pascal
Unit 1.Concepts of OOPS 2
1. #include<stdio.h>:It is used to perform input and output operations using functions scanf()
and printf().
2. #include<iostream.h>: It is used as a stream of Input and Output using cin and cout.
3. #include<string.h>:It is used to perform various functionalities related to string
manipulation like strlen(), strcmp(),strcpy(), size(), etc.
4. #include<math.h>: It is used to perform mathematical operations like sqrt(), log2(), pow(),
etc.
5. #include<iomanip.h>: It is used to access set() and setprecision() function to limit the
decimal places in variables.
6. #include<signal.h>:It is used to perform signal handling functions like signal() and raise().
7. #include<stdarg.h>:It is used to perform standard argument functions like va_start() and
va_arg(). It is also used to indicate the start of the variable-length argument list and to fetch
the arguments from the variable-length argument list in the program respectively.
8. #include<errno.h>: It is used to perform error handling operations like errno(), strerror(),
perror(), etc.
9. #include<fstream.h>:It is used to control the data to read from a file as an input and data to
write into the file as an output.
10. #include<time.h>:It is used to perform functions related to date() and time() like setdate()
and getdate(). It is also used to modify the system date and get the CPU time respectively.
11. #include<float.h>:It contains a set of various platform-dependent constants related to
floating point values. These constants are proposed by ANSI C. They allow making
programs more portable. Some examples of constants included in this header file are-
e(exponent), b(base/radix), etc.
12. #include<limits.h>:It determines various properties of the various variable types. The
macros defined in this header, limits the values of various variable types like char, int, and
long. These limits specify that a variable cannot store any value beyond these limits, for
example an unsigned character can store up to a maximum value of 255.
13. #include<assert.h>: It contains information for adding diagnostics that aid program
debugging.
14. #include<ctype.h>: It contains function prototypes for functions that test characters for
certain properties , and also function prototypes for functions that can be used to convert
uppercase letters to lowercase letters and vice versa.
15. #include<locale.h>: It contains function prototypes and other information that enables a
program to be modified for the current locale on which it’s running. It enables the computer
system to handle different conventions for expressing data such as times, dates or large
numbers throughout the world.
16. #include<setjmp.h>: It contains function prototypes for functions that allow bypassing of
the usual function call and return sequence.
17. #include<stddef.h>: It contains common type definitions used by C for performing
calculations.
Primitive Data Types: These data types are built-in or predefined and can be used directly by the
user to declare variables. Examples include int, char, float, bool, etc. The primitive data types
available in C++ are:
Integer
Character
Boolean
Floating Point
Double Floating Point
Valueless or Void
Wide Character
Derived Data Types: These data types are derived from the primitive or built-in data types. The
four main types are:
Function
Array
Pointer
Reference
Abstract or User-Defined Data Types: These data types are defined by the user. For example,
defining a class or a structure in C++. C++ provides the following user-defined data types:
Class
Structure
Union
Enumeration
Typedef
Character: The character data type is used for storing characters. The keyword used for the character
Unit 1.Concepts of OOPS 4
data type is char. Characters typically require 1 byte of memory space and range from -128 to 127
or 0 to 255.
Boolean: The boolean data type is used for storing boolean or logical values. A boolean variable can
store either true or false. The keyword used for the boolean data type is bool.
Floating Point: The floating point data type is used for storing single-precision floating point values
or decimal values. The keyword used for the floating point data type is float. Float variables
typically require 4 bytes of memory space.
Double Floating Point: The double floating point data type is used for storing double-precision
floating point values or decimal values. The keyword used for the double floating point data type is
double. Double variables typically require 8 bytes of memory space.
Void: Void means "without any value." The void data type represents a valueless entity and is used
for functions that do not return a value.
Wide Character: The wide character data type is a character data type with a size greater than the
normal 8-bit data type. It is represented by wchar_t and is generally 2 or 4 bytes long.
Actually, you do not need to place the null character at the end of a string constant. The C++
compiler automatically places the '\0' at the end of the string when it initializes the array. Let us try
to print the above-mentioned string.
Unit 1.Concepts of OOPS 5
When the above code is compiled and executed, it produces the following result
A pointer variable of the char type is treated as a string.The pointer can be assigned a character
array.when the character pointer is assigned, cout prints the complete array of characters (till it
encounters a null character).
This creates a string and stores its address in the pointer variable c. The pointer c now points to the
first character of the string "Hello." The output of the above code is Hello.
The above program takes a string from the user and displays the string in the output.
strcmp(s1, s2) returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if
4
s1>s2.
Declaring Objects: To use the data and access functions defined in the class, you need to create
objects. Syntax: ClassName ObjectName;
Accessing Data Members and Member Functions: The data members and member functions of
a class can be accessed using the dot(‘.’) operator with the object.
Public data members are accessed in the same way as described above; however, private data
members are not allowed to be accessed directly by the object. Accessing a data member depends
on the access control of that data member. This access control is determined by access modifiers in
C++. There are three access modifiers: public, private, and protected.
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.
Example 1.4: Write a program to find the area of a rectangle using a class.
#include <iostream.h>
#include <conio.h>
class rect
{
private:
int l,b;
public:
void getdata();
void putdata()//inside class
{
cout<<"length "<<l<<endl;
cout<<"breath "<<b<<endl;
cout<<"area is "<<l*b<<endl;
Unit 1.Concepts of OOPS 9
}
};
Output
enter length10
enter breath10
length 10
breath 10
area is 100
void main()
{
int i;
char s[10];
clrscr();
cout<<"Enter String:";
cin>>s;
cout<<"String is"<<s<<endl;
for(i=0;s[i]!='\0';i++)
{
if(s[i]>=65 && s[i]<=90)
s[i]=s[i]+32;
cout<<s[i];
}
getch();
}
Unit 1.Concepts of OOPS 10
2. Write a program that takes multiple strings as input. Display each string along with its
vowels, and finally, display the total number of vowels found in all strings.
#include <iostream.h>
#include <conio.h>
void main()
{
int c=0;
int total=0;
int i,j,n;
char s[6][10];
clrscr();
cout<<"HOW MANY STRING DO YOU WANT";
cin>>n;
cout<<"ENTER STRING";
for(i=0;i<n;i++)
cin>>s[i];
for(i=0;i<n;i++)
cout<<"\t\t"<<s[i]<<endl;
for(i=0;i<n;i++)
{
cout<<"\t\t";
for(j=0;s[i][j]!='\0';j++)
{
if(s[i][j]=='a'|| s[i][j]=='e'|| s[i][j]=='i'|| s[i][j]=='u'||s[i][j]=='o')
c++;
cout<<s[i][j];
}
cout<<"\t"<<c;
total+=c;
c=0;
cout<<endl;
}
cout<<"\t\t"<<"total vowels "<<total;
getch();
}
3. Print the values of a char array using a pointer.Reverse the char array using a pointer.
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
char b[10],*c;
int l,i;
clrscr();
cout<<"enter string";
Unit 1.Concepts of OOPS 11
cin>>b;
//c=&b[0];
c=b;
l=strlen(b);
getch();
#include <iostream.h>
#include <conio.h>
class std
{
private:
int r,t,p,m[5];
char name[10];
public:
void getdata();
void putdata();
}s[10];
void std::getdata()
{
cout<<"enter r";
cin>>r;
cout<<"name";
cin>>name;
t=0;
for(int i=0;i<5;i++)
{
cout<<"mark"<<i+1;
cin>>m[i];
t=t+m[i];
}
p=t/5;
}
Unit 1.Concepts of OOPS 12
void std::putdata()
{
cout<<"marksheet"<<endl;
cout<<"roll no"<<r<<endl;
cout<<"name"<<name<<endl;
for(int i=0;i<5;i++)
{
cout<<"marks"<<i+1<<" "<<m[i]<<endl;
}
cout<<"total"<<t<<endl;
cout<<"per"<<p<<endl;
}
void main()
{
clrscr();
int n;
cout<<"enter n";
cin>>n;
for(int i=0;i<n;i++)
{
s[i].getdata();
s[i].putdata();
}
getch();
}
5. Write a program to take hours, minutes, and seconds, and display the total time in seconds.
#include <iostream.h>
#include <conio.h>
class time
{
private:
int h,m,s,ts;
public:
void get();
void con();
void put()
{
cout<<"hh:"<<h<<"mm:"<<m<<"ss:"<<s<<endl;
cout<<"total second is"<<ts<<endl;
}
};
void time::get()
{
cout<<"enter h m s";
cin>>h>>m>>s;
}
void time::con()
Unit 1.Concepts of OOPS 13
{
ts=0;
ts=h*3600+m*60+s;
}
void main()
{
time t;
clrscr();
t.get();
t.con();
t.put();
getch();
}