0% found this document useful (0 votes)
3 views13 pages

Oop DS U1

This document covers the fundamental concepts of Object-Oriented Programming (OOP) and its differences from Procedural-Oriented Programming (POP), including key features such as classes, objects, and data types in C++. It also details various standard library header files required for C++, string manipulation functions, and provides examples of class definitions and member functions. Additionally, the document includes programming exercises related to string handling and class usage.

Uploaded by

ketangsuvagiya
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)
3 views13 pages

Oop DS U1

This document covers the fundamental concepts of Object-Oriented Programming (OOP) and its differences from Procedural-Oriented Programming (POP), including key features such as classes, objects, and data types in C++. It also details various standard library header files required for C++, string manipulation functions, and provides examples of class definitions and member functions. Additionally, the document includes programming exercises related to string handling and class usage.

Uploaded by

ketangsuvagiya
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/ 13

Unit 1.

Concepts of OOPS 1

Unit 1.Concepts of OOPS


1.1 Difference between procedural programming and OOPS
1.2 Various library(header) files require for C++
1.3 Data types in C++
1.4 Concepts of String
1.4.1 character Array
1.4.2 pointer to character array
1.4.3 Use of String.h and its important functions
(strcmp, strcat, strcpy, strlen, strrev )
1.5 Concepts of Class and Objects

1.1 Difference between procedural programming and OOPS

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.

Following are the important differences between OOP and POP.

No. Key OOP POP

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.2 Various library(header) files require for C++


Standard Header Files And Their Uses:

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.

1.3 Data types in C++


Data types are used to specify the type of data a variable can store. The following are the basic data
types available in C++.
Unit 1.Concepts of OOPS 3

Data types in C++ are mainly divided into three categories:

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

Primitive data types available in C++


Integer: The keyword used for integer data types is int. Integers typically require 2 bytes of memory
space and range from -2147483648 to 2147483647.

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.

1.4 Concepts of String


1.4.1 Character Array

A string is actually a one-dimensional array of characters terminated by a null character '\0'.


The following declaration and initialization create a string consisting of the word "Hello." To hold
the null character at the end of the array, the size of the character array containing the string is one
more than the number of characters in the word "Hello".

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};


If you follow the rule of array initialization,you can write the above statement as follows.
char greeting[] = "Hello";
The following is the memory presentation of the above defined string in C/C++ .

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

Example 1.1 String Initialization and Display.

When the above code is compiled and executed, it produces the following result

1.4.2 pointer to character array

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).

Example: Pointer to character array


Unit 1.Concepts of OOPS 6

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.

Example 1.2: Read and Display String Using Pointer.

The above program takes a string from the user and displays the string in the output.

1.4.3 Use of String.h and its important functions


(strcmp, strcat, strcpy, strlen, strrev )

No Function & Purpose

1 strcpy(s1, s2) copies the string s2 into the string s1.

2 strcat(s1, s2) concatenates string s2 onto the end of string s1.


3 strlen(s1) returns the length of the string s1.

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.

5 strrev(s1) returns the reverse of the string s1.


Unit 1.Concepts of OOPS 7

The following example makes use of the above-mentioned functions.

Example 1.3 String functions.

After compilation and execution, the program output will be as follows.

1.5 Concepts of Class and Objects


A class is a user-defined data type. It can be used like any other built-in data type and has data
members and member functions. Data members are the data variables, and member functions are
the functions used to manipulate these variables. Together, these data members and member
functions are called class members.

Defining Class and Declaring Objects


A class is defined in C++ using the keyword class followed by the name of the class.The body of
class is defined inside the curly brackets and terminated by a semicolon at the end.
Unit 1.Concepts of OOPS 8

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.

Member Functions in Classes


There are two ways to define a member function:

1. Inside the class definition.


2. Outside the 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.

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

}
};

void rect::getdata()//outside class


{
cout<<"enter length";
cin>>l;
cout<<"enter breath";
cin>>b;
}
void main()
{
clrscr();
rect r;
r.getdata();
r.putdata();
getch();
}

Output
enter length10
enter breath10
length 10
breath 10
area is 100

Programming exercises and solutions

1.Write a program to input string and convert uppercase characters to lowercase.


#include <iostream.h>
#include <conio.h>

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);

cout<<"\n String is";


for(i=0;i<l;i++)
{
cout<<*(c+i);
}

cout<<"\n Reverse string is ";


for(i=l-1;i>=0;i--)
{
cout<<*(c+i);
}

getch();

4. Write a program to print a marksheet using an array of object.

#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();
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy