Professional Documents
Culture Documents
1.principals of Object Oriented Programming
1.principals of Object Oriented Programming
1.principals of Object Oriented Programming
Content:
Procedure Oriented Programming (POP) verses Object Oriented
Programming (OOP).
Basic concepts of Object Oriented Programming, Object Oriented
Languages, Applications of OOP.
C verses C++, Tokens, keywords, variables, constants, basic data types.
User defined data types, type casting, operators, and expressions.
Simple C++ Program.
Control structures: Decision making statements and Loops.
Scope resolution operator, memory management operators.
Arrays, Strings and Structures in C++.
Procedure Oriented Programming (POP) verses Object Oriented Programming
(OOP)
Mainly focus on function rather than Mainly focus on object rather than
object. function.
Programs are divided into functions. Programs are divided into object.
1
Basic concepts of Object Oriented Programming
1. Objects
2. Classes
3. Data Abstraction
4. Data Encapsulation
5. Inheritance
6. Polymorphism
7. Dynamic Binding
8. Message Passing
1. Objects
An object can be thought of an entity that represent a person, thing, place, animal or any item that
the program has to handle or any user defined data.
Each object has a unique identity some definite state or characteristics and same behaviour
Object : Book
DATA :
book_name
auther
place
Functions :
getdata();
Putdata();
2. Classes :
"Class is a collection of similar types of objects which are having common properties and
common behaviour."
A class is a user defined data type which consists of data and member functions of an object.
Once class has been defined we can create any number of objects.
E.g. consider a class as fruit having objects as mango apple, orange
2
3. Data Abstraction:
"To show essential data by hiding background detail is known as data abstractions"
Abstraction refers to the act of representing the essential features without including the
background details or explanations. The process of defining a data type often called as abstract data
type [ADT] together with the concept of data hiding.
4. Data Encapsulation :
The data is not accessible to the function to the outside and only those functions which are
in the class access them. The process of protecting data from direct access is called as data hiding.
5. Inheritance:
"The ability to derive new class from old class by acquiring its properties and with its own
properties is known as inheritance".
It means one class of objects inherits the data and behaviour from another class. The
concept of inheritance provides the concept of reusability.
E.g. Vehicle is the base class and two other classes namely two wheeler and four wheeler from
vehicle class and other classes are derived from them.
Vehicle
3
6. Polymorphism:
This name comes from Greek word ‘Poly’ means Many "Morphism'- form. It plays an
important role in allowing object having different structures to share the same internal interface.
This means general class of operations may be accessed in same manner even though
actions associated with each operation may differ.
Ball Pen
Write
7. Dynamic Binding:
“Selection of function at the time of execution or Run time selection is known as dynamic
binding."
8. Message Passing:
4
Object Oriented Languages
Following are the object oriented languages.
1. C++
2. JAVA
3. Python
4. Smalltalk
Applications of OOP
5
Difference between C and C++
C C++
C does not support polymorphism, inheritance C++ supports inheritance polymorphism and
and data encapsulation. data abstraction.
6
Structure of C++ program
Main Function
<iostream.h>
<conio.h>
<math.h>
<Stack.h>
Declaring structure.
Declaring Variable.
This section can considered as sub section for the global declaration Section. Class declaration
and all methods defined here.
4. Main Function:
Each and every C++ program starts always with main function. This is entry point for all the
function. Each and every function is called indirectly through main. We can create objects of class
here.
7
Tokens
"The smallest individual and meaningful entity is called as Token"
1. Keywords
2. Variables
3. Constants
4. Data Types
1. Keywords
“Keywords are the words which are reserved and predefined words and whose meaning does
not change throughout the program."
2. Variable:
“It is the container which can contain value and it may be change”.
Syntax :
Data_type variable_name;
int a;
float b;
char c;
3. Constants:
A constant value is the one which does not change during the execution of program.
8
4. Data Type:
Structure Array
Union Function
Class Pointer
Enumerated
9
1. User Defined Data Type:
"The data types which are derived by user by using built in data type is known as user
defined data type
For example
a. Structure.
b. Union.
c. Class.
d. Enumeration.
a. Structure:
"Structure is a collection of different types of data types”
b. Union :
"Union is a collection of similar types of data types”
c. Class:
“Class is the collection of similar types of objects."
d. Enumeration:
“Enumeration (or enum) is a user defined data type in C. It is mainly used to assign
names to integral constants, the names make a program easy to read and maintain.”
1. Integer:
It is data type which represent the numbers which does not containing decimal point
2. Float
It is the data type which represent the numbers which containing decimal points
3. Char:
4. Double
Double Floating Point data type is used for storing double precision floating point values or
decimal values.
10
3. Derived Data Type:
It is the data type which is derived from the build in data type such as
1. Array
2. Function
A function is a set of statements that take inputs, do some specific computation and
produces output.
3. Pointer
Operator in C++
1. It supports all operators in C
It uses for resolving the scope and make members visible to outside word. It is use for class
defining member function outside the Class.
The scope resolution operator [::] allows access to global variables even though there is local
variable of same name with in the main () function.
The use of the double colon instructs the system that we are interested in using the global
variable name rather than the local variable name.
11
The <iostream.h> File :
The #include <iostream.h> directive is used in the program. It is library having basic
Keywords of a C++ program.
The directive causes the pre-processor to add the content of the file to the program. The
header file should be included at the beginning of all programs that uses input /output statements.
Syntax:
eg
It use to pass the input to the program or accept value in the variable by User.
Syntax:
cin>> variable-name;
e.g.
cin>> no;
By using above statement the value stored in the variable no.
12
Basic Programs of C++
1.1 Write a C++ program for find area of circle
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int r;
float area;
area=r*r*3.14;
getch();
}
OUTPUT
13
1.2 Write a C++ program to perform division and multiplication.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,add,sub,mul;
float div;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
cout<<"\n Results\n";
cout<<" \n Addition = "<<add;
cout<<" \n Subtraction = "<<sub;
cout<<" \n Multiplication = "<<mul;
cout<<" \n Division = "<<div;
getch();
}
OUTPUT
14
1.3 Write a C++ program to calculate gross salary where
DA = 75%
HRA = 20%
Gross = basic + HRA + DA
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float basic,DA,HRA,gross;
DA = (basic*75) / 100;
HRA = (basic*20) / 100;
Gross = basic + DA + HRA;
getch();
}
OUTPUT
15
1.4 Write a C++ program to demonstrate the use of Operator by evaluating the
following expression.
X1=( -b + [√b^2-4ac]) / 2a
X2= [-b+(-√b^2-4ac)] / 2a
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float a,b,c,p,q,x1,x2;
p = (b*b -4*a*c);
q = sqrt( p );
x1 = (-b+p) / (2*a);
x2 = (-b-p) / (2*a);
getch();
}
OUTPUT
16
Control Structure
1. Decision Making
2. Loops
1. Decision Making
A. If
It is use for handle single condition. It can handle true or false condition.
START
If FALSE
(Condition)
TRUE
STATEMENTS
STOP
If condition satisfies then execute statement then exit.
Syntax Example
If (condition) if (a>b)
{ {
-------------------- cout<<” \n A is greater than B”;
--------------------- Statements }
--------------------
--------------------
}
17
B. If – else
It is use for handling two conditions. If condition one satisfy then execute if loop
START
FALSE
If
(Condition)
TRUE
STATEMENTS STATEMENTS
STOP
Syntax Example
If (condition)
{ if (a>b)
-------------------- {
--------------------- Statements cout<<” \n A is greater than B”;
-------------------- }
-------------------- else
} {
else cout<<”\n B is greater than A”;
{ }
--------------------
--------------------- Statements
--------------------
--------------------
}
18
C. If – else – if Ladder
Syntax Example
If (condition 1) If (a>b && a>c)
{ {
------------------------ cout<<”\n A is greater number”;
------------------------ Statements }
------------------------ else if (b>a && b>c)
------------------------ {
} cout<<”\n B is greater number”;
else if (condition 2) }
{ else if (c>a && c>b)
------------------------ {
------------------------ Statements cout<<”\n C is greater number”;
------------------------ }
------------------------ else
} {
else if (condition 3) cout<<”\n All are equal”;
{ }
------------------------
------------------------ Statements
------------------------
------------------------
}
else
{
------------------------
------------------------ Statements
------------------------
------------------------
}
19
D. switch case
It is use for handle multiple cases and use for developing menu driven
program.
Syntax Example
switch (ch) switch (ch)
{ {
case 1: case 1:
--------------------- cout<<”\n I am in 1st case”;
--------------------- break;
break; case 2:
case 2: cout<<”\n I am in 2nd case”;
--------------------- break;
--------------------- case 3:
break; cout<<”\n I am in 3rd case”;
. . break;
case 4:
. . cout<<”\n I am in 4th case”;
. . break;
. . default:
case n: cout<<”\n Wrong Choice”;
--------------------- break;
--------------------- }
break;
default:
---------------------
---------------------
break;
}
20
2. Loop
A. While loop
It is used for handling the repetitive conditions where initialisation, condition
START
INITIALISATION
FALSE
while
(Condition)
TRUE
INCREMENT / DECREMENT
STATEMENTS
STOP
21
Syntax Example
Initialise; int I = 1;
while (condition) while (i<=10)
{ {
--------------------- cout<<”\n Cloud Computing”;
---------------------- Statements i++;
---------------------- }
Increment / Decrement;
}
Cloud Computing
Cloud Computing
Cloud Computing
Cloud Computing
Cloud Computing
Cloud Computing
Cloud Computing
Cloud Computing
Cloud Computing
Cloud Computing
22
B. Do – while loop
It is same as while loop but in do while loop first loop executed then
START
INITIALISATION
STATEMENTS
INCREMENT / DECREMENT
TRUE
while
(Condition)
FALSE
STOP
Syntax Example
initialisation; int I = 1;
do int I;
{ I = 1;
-------------------- {
-------------------- Statements cout<<”\n Cloud Computing”;
-------------------- i++;
Increment / Decrement; } while (i<=10);
} while (condition);
23
C. For loop
For is also used for representation and increment condition and init combinable.
Syntax Example
24
C++ Programs on Decision making and control loop
1.5 C++ Programs to check number is greater.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int no1,no2;
getch();
}
if(no%2 == 0)
{
cout<<"\n The given umber is Even";
}
getch();
}
25
1.7 Write a C++ program to find greatest number from two numbers.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int no1,no2;
cout<<"\n Enter the first number :";
cin>>no1;
cout<<"\n Enter the second number :";
cin>>no2;
if(no1 > no2)
{
cout<<"\n First number is greater";
}
else
{
cout<<"\n Second number is greater";
}
getch();
}
1.8 Write a C++ program to find the given number is odd or even.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int no;
26
1.9 Write a C++ program to check entered year is leaf or not.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int year;
if(year%4 == 0)
{
cout<<"\n Entered year is leaf year";
}
else
{
cout<<"\n Entered year is not leaf year";
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
i=1;
getch();
}
27
1.11 Write a C++ program to display 1 to 100 number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
i=1;
while(i <= 100)
{
cout<<i<<"\t";
i++;
}
getch();
}
28
1.13 Write a C++ program to check number is Armstrong or not.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int no, sum=0,temp,rem;
cout<<"\n Enter the number=";
cin>>no;
temp=no;
while(temp != 0)
{
rem = temp%10;
sum = sum+rem*rem*rem;
temp = temp/10;
}
if(no == sum)
{
cout<<"\n Entered number is armstrong...";
}
else
{
cout<<"\n Entered number is not armstrong...";
}
getch();
}
void main()
{
clrscr();
int i;
i=1;
do
{
cout<<"\n"<<i*19;
i++;
}while(i<=10);
getch();
}
29
1.15 Write a C++ program to display the string 10 times.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1;i<=10;i++)
{
cout<<"\n Cloud Computing ";
}
getch();
}
void main()
{
clrscr();
int i;
for(i=1;i<=100;i++)
{
if(i%2==0)
{
cout<<i<<"\t";
}
}
getch();
}
30
1.17 Write a C++ Program to display following output
*
**
***
****
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr(); int i,j;
for(i=0;i<=3;i++)
{
for(j=0;j<=i;j++)
{
cout<<" *";
}
cout<<"\n";
}
getch();
}
1.18 Write a C++ Program to display following output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr(); int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout<<" "<<j;
}
cout<<"\n";
}
getch();
}
31
1.19 Write a C++ Program to display following output
1
2 3
4 5 6
7 8 9 10
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,k;
k=1;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
cout<<" "<<k;
k++;
}
cout<<"\n";
}
getch();
}
32
1.20 Write a C++ Program for addition, subtraction, division multiplication.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,ch;
char ans;
cout<<"\n Enter the first number = ";
cin>>a;
cout<<"\n Enter the second number = ";
cin>>b;
do
{
cout<<"\n***********************";
cout<<"\n* 1. Addition *";
cout<<"\n* 2. Subtraction *";
cout<<"\n* 3. Multiplication *";
cout<<"\n* 4. Division *";
cout<<"\n***********************";
cout<<"\n\n Enter your choice = ";
cin>>ch;
switch(ch)
{
case 1: cout<<"\n Addition = "<<a+b;
break;
getch();
}
33
Array
“Collection of similar types of variables is known as array”
The Array which can only one dimension means there is only column
and multiple rows are available. Array index Starts from 0 (zero).
A[0]
A[1]
A[2]
A[3]
A[4]
A[5]
A[6]
First element stored in first position means a[0] size is 07 then last element stored in a[6].
Declaration of array:
The array declared with data type and it depends with the number stored in
It means in array the integer numbers are stored and maximum 05 elements are stored.
34
Initialisation of array
Execution it can be done with the help of for loop are as follow.
Example
Int a[5];
for (i=0;i<=4;i++)
{
Cin>>a[i];
}
35
1.21 Write a C++ Program to accept 05 elements in array and display the element.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,a[5];
for(i=0;i<=4;i++)
{
cout<<"\n a["<<i<<"] = ";
cin>>a[i];
}
for(i=0;i<=4;i++)
{
cout<<"\n a["<<i<<"] = ";
cout<<a[i];
}
getch();
}
36
1.22 Write a C++ Program to perform addition of two dimensional array elements
and display addition.
include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,a[5],b[5],c[5];
for(i=0;i<=4;i++)
{
cout<<"a["<<i<<"] = ";
cin>>a[i];
}
for(i=0;i<=4;i++)
{
cout<<"a["<<i<<"] = ";
cin>>b[i];
}
for(i=0;i<=4;i++)
{
c[i]=a[i]+b[i];
}
for(i=0;i<=4;i++)
{
cout<<"\n a["<<i<<"] = ";
cout<<c[i];
}
getch();
}
37
1.23 Write a C++ Program to search an element from array.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,a[5],flag,k;
flag=0;
for(i=0;i<=4;i++)
{
cout<<"\n a["<<i<<"] = ";
cin>>a[i];
}
for(i=0;i<=4;i++)
{
if(a[i]==k)
{
flag=1;
break;
}
else
{
flag=0;
}
}
if(flag==1)
{
cout<<"\n The element "<<k<<" is present in a[";
cout<<i<<"] location";
}
else
{
cout<<"\n The element is not present in the array :";
}
getch();
}
1.24 Write a C++ Program to sort the elements in ascending order from array.
38
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,a[7],temp;
temp=0;
for(i=0;i<=6;i++)
{
cout<<" a["<<i<<"] = ";
cin>>a[i];
}
for(i=0;i<=4;i++)
{
for(j=0;j<=6;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
getch();
}
39
2. Two Dimensional Array
Two dimensional array means handling rows and columns it with be
declared as follows.
The first size shows the rows and second sows the columns.
Int a[5][5];
40
1.25 Write a C++ Program to accept the elements in 3x3 matrixes and display the
elements.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,a[3][3];
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
cout<<" a["<<i<<"] ["<<j<<"] = ";
cin>>a[i][j];
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<"\n\n";
}
getch();
}
41
1.26 Write a C++ Program to add two matrices and display the addition.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,a[3][3],b[3][3],c[3][3];
cout<<"\n Enter element in first matrix :\n";
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
cout<<" a["<<i<<"] ["<<j<<"] = ";
cin>>a[i][j];
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
cout<<" b["<<i<<"] ["<<j<<"] = ";
cin>>b[i][j];
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
cout<<"\n The addition of matrix :\n";
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
cout<<"\t"<<c[i][j];
}
cout<<"\n\n";
}
getch();
}
42
1.27 Write a C++ Program to multiply two 3x3 matrices and display the resultant
array.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,k,a[3][3],b[3][3],c[3][3];
int sum=0;
cout<<"\n Enter element in first matrix :\n";
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
cout<<" a["<<i<<"] ["<<j<<"] = ";
cin>>a[i][j];
}
}
cout<<"\n Enter element in second matrix :\n";
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
cout<<" b["<<i<<"] ["<<j<<"] = ";
cin>>b[i][j];
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
for(k=0;k<3;k++)
{
sum=sum+a[i][k]*b[k][j];
}
c[i][j]=sum;
}
}
cout<<"\n The multiplication of matrices :\n";
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
cout<<"\t"<<c[i][j];
}
cout<<"\n\n";
}
getch();
}
43
Structure:
“Structure is the collection of different types of variable”.
Structure will be declared are as follows by using following syntax.
Syntax Example
struct struct_name
{
Variable 1; struct student
Variable 2; {
. . Int roll;
. . char name[10];
. . float per;
. . };
Variable n;
};
Object can be declared are as follows.
1. At the time of declaration of structure.
Syntax Example
struct struct_name
{
Variable 1; struct student
Variable 2; {
. . Int roll;
. . char name[10];
. . float per;
. . }s;
Variable n;
}object_name;
Syntax Example
struct struct_name
{
Variable 1; struct student
Variable 2; {
. . Int roll;
. . char name[10];
. . float per;
. . }s1,s2,s3;
Variable n;
}obj1,obj2,……,objn;
44
2. In the main function.
structure_name object_name;
student s;
Object.variable_name;
s.roll;
45
1.28 Write a C++ Program to declare structure students having data member roll,
name, and percentage accept and display data for one student.
#include<iostream.h>
#include<conio.h>
struct student
{
int roll;
char name[20];
float per;
}s;
void main()
{
clrscr();
cout<<"\n Enter roll number of student : ";
cin>>s.roll;
cout<<"\n Enter name of student : ";
cin>>s.name;
cout<<"\n Enter percentage of student : ";
cin>>s.per;
cout<<"\n\n\n Name = "<<s.name;
cout<<"\n Roll Number = "<<s.roll;
cout<<"\n Percentage = "<<s.per;
getch();
}
1.29 Write a C++ program to declare structure employee accept and display data
for employee.
#include<iostream.h>
#include<conio.h>
struct employee
{
int emp_id;
char name[20];
float sal;
}e;
void main()
{
clrscr();
cout<<"\n Enter employee ID of employee : ";
cin>>e.emp_id;
cout<<"\n Enter name of employee : ";
cin>>e.name;
cout<<"\n Enter salary of employee : ";
cin>>e.sal;
cout<<"\n\n\n Name of emplyee = "<<e.name;
cout<<"\n Employee ID = "<<e.emp_id;
cout<<"\n Salary = "<<e.sal;
getch();
}
46
1.30 Write a C++ program to declare structure book accept and display information
of book.
#include<iostream.h>
#include<conio.h>
struct book
{
char auther[40];
char name[40];
float price;
}b;
void main()
{
clrscr();
cout<<"\n Enter the book name : ";
cin>>b.name;
cout<<"\n Enter the auther name : ";
cin>>b.auther;
cout<<"\n Enter the price of book : ";
cin>>b.price;
cout<<"\n\n\n Name of book = "<<b.name;
cout<<"\n Auther of book = "<<b.auther;
cout<<"\n Price of book = "<<b.price;
getch();
}
1.31 Write a C++ program to declare structure vehicle accept and display
information of vehicle.
#include<iostream.h>
#include<conio.h>
struct vehicle
{
int cc;
char model[20];
float price;
}v;
void main()
{
clrscr();
cout<<"\n Enter the vehicle name : ";
cin>>v.model;
cout<<"\n Enter the cubik capacity of vehicle : ";
cin>>v.cc;
cout<<"\n Enter the price of vehicle : ";
cin>>v.price;
cout<<"\n\n\n Name of vehicle = "<<v.model;
cout<<"\n Cubik Capasity of Vehicle = "<<v.cc;
cout<<"\n Price of vehicle = "<<v.price;
getch();
}
47
1.32 Write a C++ program to declare structure students accept and display
information of two students.
#include<iostream.h>
#include<conio.h>
struct student
{
int roll;
char name[20];
float per;
}s1,s2;
void main()
{
clrscr();
cout<<"\n Enter roll number of student : ";
cin>>s1.roll;
cout<<"\n Enter name of student : ";
cin>>s1.name;
cout<<"\n Enter percentage of student : ";
cin>>s1.per;
cout<<"\n Enter roll number of student : ";
cin>>s2.roll;
cout<<"\n Enter name of student : ";
cin>>s2.name;
cout<<"\n Enter percentage of student : ";
cin>>s2.per;
cout<<"\n\n Information of first student :";
cout<<"\n\n Name = "<<s1.name;
cout<<"\n Roll Number = "<<s1.roll;
cout<<"\n Percentage = "<<s1.per;
cout<<"\n\n Information of second student :";
cout<<"\n\n Name = "<<s2.name;
cout<<"\n Roll Number = "<<s2.roll;
cout<<"\n Percentage = "<<s2.per;
getch();
}
48
1.33 Write a C++ program to declare structure staff accept and display information
of two staff.
#include<iostream.h>
#include<conio.h>
struct staff
{
int id;
char desig[20];
float sal;
}f1,f2;
void main()
{
clrscr();
cout<<"\n\n Enter the data for first staff ";
cout<<"\n Enter the staff id, designation and salary";
cin>>f1.id>>f1.desig>>f1.sal;
cout<<"\n\n Enter the data for second staff ";
cout<<"\n Enter the staff id, designation and salary";
cin>>f2.id>>f2.desig>>f2.sal;
getch();
}
49
1.34 Write a C++ program to declare structure car accept and display information of
ten car.
#include<iostream.h>
#include<conio.h>
struct car
{
int no;
char compony[20];
float price;
}c[10];
void main()
{
clrscr();
int i;
getch();
}
50
1.35 Write a C++ program to declare structure furniture accepts and display
information of five furniture.
#include<iostream.h>
#include<conio.h>
struct furniture
{
int height,width;
char color[20];
float price;
}f[5];
void main()
{
clrscr();
int i;
getch();
}
51
1.36 Write a C++ program to declare structure city having data members name,
population and area and display the city having population greater than
50000 [Accept information of 10 cities].
#include<iostream.h>
#include<conio.h>
struct city
{
char name[20];
int pop;
float area;
}c[10];
void main()
{
int i;
clrscr();
for(i=0;i<=
9;i++)
{
cout<<"\n Enter the information"<<i<<"city :";
cin>>c[i].name>>c[i].pop>>c[i].area;
}
for(i=0;i<=9;i++)
{
if((c[i].pop)>50000)
{
cout<<c[i].name<<c[i].pop<<c[i].area;
}
}
getch();
}
52
C++ Strings
The C-style character string originated within the C language and continues to be supported within
C++. This string is actually a one-dimensional array of characters which is terminated by
a null character '\0'. Thus a null-terminated string contains the characters that comprise the string
followed by a null.
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, then you can write the above statement as follows −
char greeting[] = "Hello";
Following is the memory presentation of above defined string in C/C++ −
Actually, you do not 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
above-mentioned string –
#include <iostream>
int main () {
return 0;
}
When the above code is compiled and executed, it produces the following result −
Greeting message: Hello
53
C++ supports a wide range of functions that manipulate null-terminated strings −
1 strcpy(s1, s2);
2 strcat(s1, s2);
3
strlen(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.
#include <iostream>
#include <cstring>
int main () {
Output-
char str1[10] = "Hello"; strcpy( str3, str1) : Hello
char str2[10] = "World"; strcat( str1, str2): HelloWorld
char str3[10]; strlen(str1) : 10
int len ;
return 0;
}
54
The String Class in C++
The standard C++ library provides a string class type that supports all the operations mentioned
above, additionally much more functionality. Let us check the following example −
#include <iostream>
#include <string>
int main () {
return 0;
}
When the above code is compiled and executed, it produces result something as follows −
str3 : Hello
str1 + str2 : HelloWorld
str3.size() : 10
#include <iostream.h>
#include <string.h>
int main ()
{
char frst [20] = "Programming";
char sec [20] = "Programming";
cout<<"Content of frst string: " <<frst <<"\n";
cout<<"Content of sec string: " <<sec <<"\n";
int result = strcmp (frst, sec);
cout<<" String comparison between " <<frst <<" and " <<sec<<" : " <<result <<"\n";
return 0;
}
55