Lab Book

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 10

Section-I: Object Oriented Concepts Through CPP

1. Beginning with C++

Q1)Write a C++ program to display first ‘n’ numbers of Fibonacci series

#include <iostream>
using namespace std;

int main() {
int n, t1 = 0, t2 = 1, nextTerm = 0;

cout << "Enter the number of terms: ";


cin >> n;

cout << "Fibonacci Series: ";

for (int i = 1; i <= n; ++i)


{
if(i == 1)
{
cout << t1 << ", ";
continue;
}
if(i == 2)
{
cout << t2 << ", ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;

cout << nextTerm << ", ";


}
return 0;
}

2 Operators and Functions in C++

Q2)Write a C++ program to accept length and width of a rectangle. Calculate and
display
perimeter as well as area of a rectangle by using Inline function

#include<iostream.h>
#include<conio.h>
class rectangle
{
float l,w;
public:
void getdata()
{
cout<<"\nEnter length of rectangle : ";
cin>>l;
cout<<"\nEnter width of rectangle : ";
cin>>w;
}
inline void Peri()
{
cout<<"\nPerimeter of rectangle = "<<2*(l+w);
}
inline void Area()
{
cout<<"\nArea of rectangle = "<<l*w;
}
};
void main()
{
clrscr();
rectangle obj;
obj.getdata();
obj.Peri();
obj.Area();
getch();
}

3 Classes and Objects

Q3)Write a C++ program to create two classes Array1 and Array2 with an integer
array as a
data member. Write necessary member functions to accept and display array elements
of
both the classes. Find and display maximum of both the array. (Use Friend function)

#include<conio.h>
#include<iostream.h>
class arr2;
class arr1
{
public:
int n1,i,*a;

void accept()
{
cout<<"Enter how many element in an array: ";
cin>>n1;
a=new int[n1];

for(i=0;i<n1;i++)
{
cout<<"Enter an element :- ";
cin>>a[i];
}
}
void disp()
{
for(i=0;i<n1;i++)
{
cout<<a[i]<<" ";
}

}
friend void min(arr1 a1,arr2 a2);
};
class arr2
{
public:
int n2,i,*b; // n2=no , *b=pointer b

void accept()
{
cout<<"\nEnter how many element in an array: ";
cin>>n2;
b=new int[n2];

for(i=0;i<n2;i++)
{
cout<<"Enter an element :- ";
cin>>b[i];
}
}
void disp()
{
for(i=0;i<n2;i++)
{
cout<<b[i]<<" ";
}
}
friend void mini(arr1 a1,arr2 a2);
};
void min(arr1 a1,arr2 a2)
{
int s,i;
s=a1.a[0];
for(i=0;i<a1.n1;i++)
{
if (s>a1.a[i])
s=a1.a[i];
}

int s1;
s1=a2.b[0];
for(i=0;i<a2.n2;i++)
{
if (s1>a2.b[i])
s1=a2.b[i];
}
if(s<s1)
cout<<"\nMINIMUM from 2 array is: "<<setw(2)<<s;
else
cout<<"\nMINIMUM from 2 array is: "<<setw(2)<<s1;
}
int main()
{
arr1 a1;
arr2 a2;
a1.accept();
a1.disp();
a2.accept();
a2.disp();
min(a1,a2);
getch();
return(0);
}

4 Constructors and Destructors


Q4)Write a C++ program to create a class MyDate with three data members as dd, mm,
yyyy.
Create and initialize the object by using parameterized constructor and display
date in dd#mon-yyyy format. (Input: 19-12-2014 Output: 19-Dec-2014).(Use the
concept of dynamic
initialization of object)

#include<iostream.h>
#include<conio.h>
class date
{
int dd,mm,yy;
public:
date(int,int,int);
void show();
};
date::date(int d,int m,int y)
{
dd=d;
mm=m;
yy=y;
}
void date::show()
{
cout< switch(mm)
{
case 1 :cout<<" Jan / ";
break;
case 2 :cout<<" Feb / ";
break;
case 3 :cout<<" Mar / ";
break;
case 4 :cout<<" Apr / ";
break;
case 5 :cout<<" May / ";
break;
case 6 :cout<<" Jun / ";
break;
case 7 :cout<<" Jul / ";
break;
case 8 :cout<<" Aug / ";
break;
case 9 :cout<<" Sep / ";
break;
case 10 :cout<<" Oct / ";
break;
case 11 :cout<<" Nov / ";
break;
case 12 :cout<<" Dec / ";
break;
}
cout< }
void main()
{
int dd,mm,yy;
clrscr();
cout<<"\n Enter date : ";
cout<<"\n Day : ";
cin>>dd;
cout<<"\n Month : ";
cin>>mm;
cout<<"\n Year : ";
cin>>yy;
if(mm>12)
{
cout<<"\n Invalid Month entered...";
}
else
{
date d(dd,mm,yy);
d.show();
}
getch();
}

5 Inheritance

Q5)Create a base class Student(Roll_No, Name) which derives two classes


Academic_Marks(Mark1, Mark2, Mark3) and Extra_Activities_Marks(Marks). Class
Result(Total_Marks, Grade) inherits both Academic_Marks and Extra_Activities_Marks
classes. (Use Virtual Base Class)
Write a C++ menu driven program to perform the following functions:
i. Build a master table.
ii. Calculate Total_marks and grade

#include<conio.h>
#include<iostream.h>
class student
{
protected:
int rno;
char name[10];
public:
void accept()
{
cout<<"\nEnter roll no if sudent :
";
cin>>rno;
cout<<"\nEnter the name of
student :";
cin>>name;
cout<<endl;
}
void display()
{
cout<<"\nRoll no : "<<rno<<"\t Name
: "<<name<<endl;
}
};
class Academic_Mark:public virtual student
{
protected:
int mark1,mark2,mark3;
public:
void accept_A()
{
cout<<"\nEnter the 3 subject
marks :";
cin>>mark1>>mark2>>mark3;
}
void display_A()
{
cout<<"\nMarks sub1 :"<<mark1<<"\
nMarks sub2 :"<<mark2<<"\nMarks sub3 :"<<mark3;
}
};
class Extra_Activity : public virtual student
{
protected:
int mark;
public:
void accept_E()
{
cout<<"\nEnter Extra Activity
mark :";
cin>>mark;
}
void display_E()
{
cout<<"\nEnter Extra activity mark
:"<<mark<<endl;
}
};

class result: public Academic_Mark,public Extra_Activity


{
public:
int total;
char grade[10];
float per;

void cal_res()
{
total=mark1+mark2+mark3+mark;
per=(total/3);
if(per>=70)

strcpy(grade,"distinction");
else
if(per<70 &&
per>=60)
strcpy(grade,"A+");
else
if(per<60 &&
per>=50)
strcpy(grade,"A");
else if(per<50 && per>=40)
strcpy(grade,"B");
else

strcpy(grade,"Fail");
cout<<"\n
==============================";
cout<<"\nTotal :"<<total<<"\
nGrade :"<<grade<<endl;
}
};

void main()
{
clrscr();
int n,ch;
result obj;
do
{
cout<<"\n 1.student info \n 2.Academic mark \n 3.Extra activity
mark \n 4.Result \n 0.Exit";
cout<<"\n Enter your choice : ";
cin>>ch;
switch(ch)
{ case 1: obj.accept();
break;
case 2: obj.accept_A();
break;
case 3: obj.accept_E();
break;
case 4: obj.display();
obj.display_A();
obj.display_E();
obj.cal_res();
break;
case 0: break;
default: cout<<"\n Invalid choice :
";
}
}while(ch!=0);

getch();
}

6 Polymorphism

Q6)Write a C++ program to calculate area of cone, sphere and circle by using
function
overloading.

#include<iostream.h>
using namespace std;
int area(int);
int area(int,int);
float area(float);
float area(float,float);
int main()
{
int s,l,b;
float r,bs,ht;
cout<<"Enter side of a square:";
cin>>s;
cout<<"Enter length and breadth of rectangle:";
cin>>l>>b;
cout<<"Enter radius of circle:";
cin>>r;
cout<<"Enter base and height of triangle:";
cin>>bs>>ht;
cout<<"Area of square is"< cout<<"\nArea of rectangle is "< cout<<"\nArea of circle
is "< cout<<"\nArea of triangle is "<
}
int area(int s)
{
return(s*s);
}
int area(int l,int b)
{
return(l*b);
}
float area(float r)
{
return(3.14*r*r);
}
float area(float bs,float ht)
{
return((bs*ht)/2);
}

PHP

1)Introduction to Object Oriented Programming with PHP


Q1)Create a class named DISTANCE with feet and inches as data members. The class
has
the following member functions: convert_feet_to_inch() , convert_inch_to_feet() .
Display options using radio button and display conversion on next page.

<?php
interface Cyl
{
function area();
function volume();
}
class Cylinder implements Cyl
{
public $PI=3.14;
public $a;
public $r;
public $h;
function __construct($r,$h)
{
$this->r=$r;
$this->h=$h;
}
function area()
{
$this->a=2*$this->PI*($this->h*$this->r);
echo"Area of Cylinder= ".$this-
>a."<br>";
}
function volume()
{
$this->a=$this->PI*$this->r*$this->r*$this->h;
echo"Volume of Cylinder= ".$this-
>a."<br>";
}
}
$c=new Cylinder(10,7);
$c->area();
$c->volume();
?>

2) To study Web Techniques


Q2)Write a PHP Script to display Server information in table format (Use
$_SERVER).
<html>
<body>
<table border="1">
<tr>
<th>PHP_SELF</th>
<th>SERVER_NAME</th>
<th>HTTP_HOST</th>
<th>SCRIPT_NAME</th>
</tr>
<tr>
<?php
echo "<td>" .$_SERVER['PHP_SELF'] ."</td>";
echo "<td>" .$_SERVER['SERVER_NAME'] ."</td>";
echo "<td>" .$_SERVER['HTTP_HOST'] ."</td>";
echo "<td>".$_SERVER['SCRIPT_NAME']."</td>";
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";

?>
</tr>
</table>
</body>
</html>

3)XML
Q3)Write a AJAX program to display the selected course information from the list
given in XML
file and show the following output.
<?xml version='1.0' encoding ='UTF-8' ?>
<bookstore>
<books category="technical">
<book_no>1</book_no>
<book_name>def</book_name>
<author_name>xxx</author_name>
<price>100</price>
<year>1990</year>
</books>
<books category="Cooking">
<book_no>2</book_no>
<book_name>ccc</book_name>
<author_name>aaa</author_name>
<price>200</price>
<year>1950</year>
</books>
<books category="YOGA">
<book_no>3
<book_name>ddd</book_name>
<author_name>zzz</author_name>
<price>150</price>
<year>2016<year>
</books>
</bookstore>

You might also like