CSC213 Object Oriented Programming-Lab Manual-Sol
CSC213 Object Oriented Programming-Lab Manual-Sol
CSC213 Object Oriented Programming-Lab Manual-Sol
OBJECT ORIENTED
PROGRAMMING
CSC-213
SPRING 2020
OOPs Lab Manual
Lab Manual
2
OOPs Lab Manual
Table of Contents
Experiment
Experiment Title Date Signature
No.
The objective of this lab is to practice with input, 20/01/2021
1. output and variables and to familiarize students with
IDE interface.
The objective of this lab is to practice with variables
2. 27/01/2021
declaration, class components and compiling code.
To create a program with conditional IF-Statement
3. and IF-ELSE-IF Ladder and compare variables to
display the result.
To create a program with FOR loop for repeated
4.
processing of s statement.
To create a two dimensional array, insert values in
5.
the array and display the result.
To implement Switch statement and develop an
6.
arithmetic calculator.
To demonstrate the concept of class and declaration
7.
of objects with a class and display the output.
To develop a constructor, to initialize object and
8.
display output.
To implement inheritance concept using class and
9.
subclass and display the output.
To implement the concept of polymorphism
10. (dynamic Binding or Late Binding) and display the
output.
To implement exception handling using TRY-
11
CATCH statement and display the result.
To implement the concept of collection in java and
12.
run the program
3
OOPs Lab Manual
LAB EXPERIMENT # 1
Objective: The objective of this lab is to practice with input, output and variables
and to familiarize students with IDE interface.
Example 1:
#include <iostream>
using namespace std;
int main( ) {
cout << " Welcome to C++ tutorial " << endl;
}
Output:
Example 2:
#include <iostream>
using namespace std;
int main( ) {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "Your age is: " << age << endl;
}
Output:
4
OOPs Lab Manual
Task 1:
(Print a table) Write a program that displays the following table:
Source code:
#include<iostream>
using namespace std;
main()
{
int i;
cout << "a\ta^2\ta^3" <<endl;
for(i=1;i<=4;i++)
{
cout <<i <<"\t" <<i*i <<"\t" <<i*i*i <<endl;
}
}
Execution:
5
OOPs Lab Manual
Task 2:
(Average speed in miles) Assume a runner runs 14 kilometers in 45 minutes
and 30 seconds. Write a program that displays the average speed in miles per
hour. (Note that 1 mile is 1.6 kilometers.)
Source code:
#include <iostream>
using namespace std;
main()
{
float miles;
float milperhour;
float mins=45;
float totalsec;
float givensec=30;
int kilometer=14;
float hours;
mins=45;
totalsec=(mins*60)+givensec;
hours=totalsec/3600;
miles=kilometer/1.6;
milperhour=(miles/hours);
6
OOPs Lab Manual
cout << "The sprinter has covered 14 kilometers in 45 minutes and 30 seconds" <<endl
<<endl;
cout << "Converting given time into hours " <<endl <<endl;
cout << "After converting time and kilometers into miles the duration is: " <<endl
<<endl;
cout << "Miles per hour: " << milperhour;
}
Execution:
7
OOPs Lab Manual
LAB EXPERIMENT # 2
Objective: The objective of this lab is to practice with variables declaration, class
components and compiling code.
Example 1:
#include <iostream>
using namespace std;
int main( ) {
char ary[] = "Welcome to C++ tutorial";
cout << "Value of ary is: " << ary << endl;
}
Output:
8
OOPs Lab Manual
Example 2:
#include <iostream>
using namespace std;
int main( ) {
int x=5,b=10; //declaring 2 variable of integer type
float f=30.8;
char c='A';
cout << "Sum = " << x+b << endl;
cout << "Float = " << f << endl;
cout << "Character = " << c << endl;
}
Output:
9
OOPs Lab Manual
Task 1:
Write a program that displays the following equation:
-7 – 4a = -a + 8
Source code:
10
OOPs Lab Manual
Execution:
Task 2:
Write a program that displays the following equation:
4c = 8 - 4c
Source code:
11
OOPs Lab Manual
Execution:
LAB EXPERIMENT # 3
Example 1: If Statement
// Program to print positive number entered by the user
// If user enters negative number, it is skipped
#include <iostream>
using namespace std;
int main()
{
12
OOPs Lab Manual
int number;
cout << "Enter an integer: ";
cin >> number;
// checks if the number is positive
if ( number > 0)
{
cout << "You entered a positive integer: " << number << endl;
}
cout << "This statement is always executed.";
return 0;
}
Output:
13
OOPs Lab Manual
else
{
cout << "You entered a negative integer: " << number << endl;
}
cout << "This line is always printed.";
return 0;
}
Output:
14
OOPs Lab Manual
15
OOPs Lab Manual
Task 1:
Write a program that a student will not be allowed to sit in exam if his/her
attendance is less than 75%.
Take following input from user
o Number of classes held
o Number of classes attended.
And print percentage of class attended
In addition, print student is allowed to sit in exam or not.
Source code:
16
OOPs Lab Manual
Execution:
Task 2:
Write a program and display the answer of the expressions:
If
x=2
y=5
z=0
Then find values of the following expressions:
x == 2
x != 5
x != 5 && y >= 5
z != 0 || x == 2
!(y < 10)
Source code:
17
OOPs Lab Manual
// statement 1 : (x==2) will give 1 if x==2 else it will give 0 on console screen
outcome = (x==2) ? true:false;
cout << "The outcome of the statement (x==2) is : " << outcome <<endl
<<endl;
// statement 5 : !(y<10) will give 1 if y is not less than 10 else it will give 0 on
console screen
outcome = !(y<10) ? true:false;
cout << "The outcome of the statement !(y<10) is : " << outcome <<endl
<<endl;
18
OOPs Lab Manual
Execution:
Task 3:
Write a program to check whether a entered character is lowercase (a to z ) or
uppercase ( A to Z ).
Source code:
19
OOPs Lab Manual
else if(c>=48 && c<=57) // this block is executed if given character is a number
{
cout << "you have entered a numerical digit !!";
}
else // this block is executed checks if given character is a special character
{
cout << "you have not entered a letter or a digit !!!!!!!!";
}
} // main function ends here
Execution:
Task 4:
Write a program to create grade book using IF Statement.
Source code:
20
OOPs Lab Manual
int sci;
int eng;
int math;
int avg;
int obtained;
//**********
cout << "***** Grade sheet *****" << endl << endl ;
cout << "Enter your science marks : " ;
cin >> sci ;
cout << endl << "Enter your english marks : " ;
cin >> eng ;
cout << endl << "Enter your maths marks : " ;
cin >> math ;
obtained=sci+eng+math; // This line is calculating total obtained marks of all subjects
combined
avg=(obtained*100)/300; // This line is calculating average % of your session
cout << endl << "your obtained average percenatage is : " << avg << "%" ;
if(avg>=90) // This block will be executed if a student has secured 90% and above
{
cout <<endl << "You secured 'A+' grade sucessfully " ;
cout << endl << endl << "***Excellent***" ;
}
else if(avg>=80) // This block will be executed if a student has secured 80% and above
{
cout <<endl << "You secured 'A' grade sucessfully " ;
cout << endl << endl << "***Very Good***" ;
}
else if(avg>=70) // This block will be executed if a student has secured 70% and above
{
cout <<endl << "You secured 'B' grade sucessfully " ;
cout << endl << endl << "***Good job***" ;
}
else if(avg>=60) // This block will be executed if a student has secured 60% and above
{
cout <<endl << "You secured 'C' grade sucessfully " ;
cout << endl << endl << "***Need some improvement***" ;
}
else if(avg>=50) // This block will be executed if a student has secured 50% and above
{
cout <<endl << "You secured 'D' grade sucessfully " ;
cout << endl << endl << "***Work Hard next time***" ;
}
21
OOPs Lab Manual
else // This block will be executed if a student has failed session obtaining less than
50%
{
cout << endl << "You have failed this examination with an 'F' grade ";
cout << endl << endl << "***Repeat this exam with great enthusiasm and progressive
approach***" ;
}
} // main function ends here
Execution phase 1:
Execution phase 2:
22
OOPs Lab Manual
LAB EXPERIMENT # 4
#include <iostream>
using namespace std;
int main()
{
int i, n, factorial = 1;
23
OOPs Lab Manual
cin >> n;
Output:
Task 1:
Write a C++ program to print all odd number between 1 to 100.
Source code:
24
OOPs Lab Manual
Execution:
Task 2:
Write a C++ program to enter any number and check whether it is Prime
number or not.
Source code:
25
OOPs Lab Manual
// integer declarations
//*****
int no,i;
int c=0;
//*****
26
OOPs Lab Manual
else // this block is executed when all above conditions are not met
{
cout << endl << "Negative numbers are non-prime
numbers !!!!";
}
Execution phase 1:
27
OOPs Lab Manual
Execution phase 2:
Task 3:
Write a C++ program to enter any number and calculate sum of all natural
numbers between 1 to n.
28
OOPs Lab Manual
Source code:
// integer declarations
// ******
int no,i;
int sum=0;
// ******
cout << endl << "The sum calculated uptil the no " << no << " is : " << sum;
29
OOPs Lab Manual
Execution:
30
OOPs Lab Manual
LAB EXPERIMENT # 5
Objective: To create a two dimensional array, insert values in the array and
display the result.
int main()
{
int test[3][2] =
{
{2, -5},
{4, 0},
{9, 1}
};
return 0;
}
Output:
31
OOPs Lab Manual
Task 1:
Write a C++ Program to Store value entered by user in three-dimensional
array and display it
Source code:
32
OOPs Lab Manual
Execution:
Task 2:
Write a C++ Program to store temperature of two different cities for a week
and display it.
Source code:
33
OOPs Lab Manual
34
OOPs Lab Manual
Execution phase 1:
35
OOPs Lab Manual
Execution phase 2:
Execution phase 3:
36
OOPs Lab Manual
Task 3:
Write a C++ program to find the largest element of a given array of integers.
Source code:
37
OOPs Lab Manual
int highest=0;
int size;
/**************/
Execution:
38
OOPs Lab Manual
39
OOPs Lab Manual
LAB EXPERIMENT # 6
#include <iostream>
using namespace std;
int main()
{
char o;
float num1, num2;
switch (o)
{
case '+':
cout << num1 << " + " << num2 << " = " << num1+num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1-num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1*num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1/num2;
break;
40
OOPs Lab Manual
default:
// operator is doesn't match any case constant (+, -, *, /)
cout << "Error! operator is not correct";
break;
}
return 0;
}
Output:
Task 1:
Write a C++ program that have some grades of students and you need to
display, like following,
Grade A = YOU VERY GOOD
Grade B = EXCELLENT
Grade C = EXCELLENT
41
OOPs Lab Manual
Grade F = WORST
(**if user enter any other letter programme should display “ERROR” message!**)
Source code:
public: // it can be
acessed from main function
char a; // switch
variable
string name;
/***********************************************/
// methods for grades according to equivalent letters
Agrade()
{
cout<< endl <<"Excedding Expection";
}
Bgrade(){
cout << endl <<"Good performance";
}
Cgrade(){
cout<< endl <<"Not bad";
}
Fgrade(){
cout<< endl <<"Try your max potential next time";
}
/*************************************************/
};
main()
{ // main function starts here
grade g1;
42
OOPs Lab Manual
{
cout << endl << "------------------------------------------------";
cout <<endl << "------------------------------------------------";
cout <<endl << g1.name << " have met " ;
g1.Agrade();
break;
}
case 'B':
case'b': // this block executes if (b) grade is
obtained
{
cout << endl << "------------------------------------------------";
cout <<endl << "------------------------------------------------";
cout <<endl << g1.name << " have shown" ;
g1.Bgrade();
break;
}
case 'C': // this block executes if (c) grade is
obtained
case'c':
{
cout << endl << "------------------------------------------------";
cout <<endl << "------------------------------------------------";
cout <<endl << g1.name << " performance is " ;
g1.Cgrade();
43
OOPs Lab Manual
break;
}
case 'F': // this block executes if (f) grade is
obtained
case'f':
{
cout << endl << "------------------------------------------------";
cout <<endl << "------------------------------------------------";
cout << endl << g1.name << " you should " ;
g1.Fgrade();
break;
}
default: // this block executes if none of
above grades are obtained
{
Execution:
44
OOPs Lab Manual
Task 2:
Write a C++ program to develop an arithmetic calculator.
Source code:
45
OOPs Lab Manual
46
OOPs Lab Manual
}
};
47
OOPs Lab Manual
**********************************************************
***************************************/
cout << endl << endl << "1. add" << endl << "2. multiply" << endl
<< "3. divide" << endl << "4. subtract" << endl << "5. factorial" <<
endl << "6. x^y" ;
cout << endl <<"7. sin,cos,tan " << endl << "8. random numbers
between no " << n1 << " and " << n2 ;
cout << endl << endl;
/
**********************************************************
**********************************************************
***************************************/
cin >> sel; // getting switch case selection variable from the user to
perform the equivalent airthmetic operation
switch(sel)
{ // switch case starts
case 1: // this block executes when user provides 1 as input
cout <<endl << "The result of addition between these two
numbers is : " << c1.add(n1,n2) ;
break;
case 2: // this block executes when user provides 2 as input
cout <<endl << "The result of mutiplication between these two
numbers is : " << c1.mult(n1,n2) ;
break;
case 3: // this block executes when user provides 3 as input
cout <<endl << "The result of division between these two
numbers is : " << c1.div(n1,n2) ;
break;
case 4: // this block executes when user provides 4 as input
cout <<endl << "The result of subtraction between these two
numbers is : " << c1.sub(n1,n2) ;
break;
case 5: // this block executes when user provides 5 as input
48
OOPs Lab Manual
cout <<endl << "The factorial of first number : " << n1 << "
is : " << c1.fact(n1) << " and second number : " << n2 << " is : "
<<c1.fact(n2) ;
break;
case 6: // this block executes when user provides 6 as input
cout <<endl << "The result of no2 : " << n2 << " as a power of
no1 " << n1 << " is : " << c1.result(n1,n2) ;
break;
case 7: // this block executes when user provides 7 as input
cout <<endl << "The sin,cos,tan of no1: " << n1 << " is : " <<
c1.sine(n1) << "\t";
cout << c1.cosine(n1) << "\t";
cout << c1.tangent(n1);
cout <<endl << "The sin,cos,tan of no2: " << n2 << " is : " <<
c1.sine(n2) << "\t";
cout << c1.cosine(n2) << "\t";
cout << c1.tangent(n2);
break;
case 8: // this block executes when user provides 8 as input
cout <<endl << "A random number between no " << n1 << "
and no " << n2 << " is : " << c1.random(n1,n2);
break;
default: // this block executes when user provides any other
input other than (1-8)
cout <<"invalid choice !!!!";
} // switch case ends
} // main functions ends here
Execution:
49
OOPs Lab Manual
50
OOPs Lab Manual
LAB EXPERIMENT # 7
Example:
#include <iostream>
using namespace std;
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main() {
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Volume of Box2 : " << volume <<endl;
return 0;
}
Output:
51
OOPs Lab Manual
First part
Second part
52
OOPs Lab Manual
Task 1:
Write a C++ program to print the area and perimeter of a triangle having
sides of 3, 4 and 5 units by creating a class named 'Triangle' with a function to
print the area and perimeter.
Source code:
/***************************************/
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
/***************************************/
};
int main() // main functions starts here
{
Box Box1; // first object of class(Box)
Box Box2; // second object of class(Box)
double volume = 0.0; // initializing volume with zero to filter out any
garbage value
// box 1 specification inside class(Box)
///-----------------///
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
///----------------///
///----------------///
// box 2 specification inside class(Box)
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
///----------------///
// calculation for volume of box 1
///------------------------------------------------///
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Volume of Box1 : " << volume <<endl;
53
OOPs Lab Manual
///------------------------------------------------///
///------------------------------------------------///
// calculation for volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Volume of Box2 : " << volume <<endl;
///------------------------------------------------///
return 0;
} // main function ends here
Execution phase:
Task 2:
Create a class named 'Student' with a string variable 'name' and an integer
variable 'roll_no'. Assign the value of roll_no as '2' and that of name as
"John" by creating an object of the class Student.
Source code:
54
OOPs Lab Manual
public: // acess specifier is (Public) (it can be acesses anywhere inside the
program)
/***********/
string name;
int roll_no;
/***********/ // variable declaration
};
main()
{ // main function starts
student s1; // object is created with instance (s1)
s1.roll_no=2; // roll_no variable is assigned with (2)
s1.name="John"; // name variable is assigned with ("john")
cout << "Name of the student is : " << s1.name; // name variable is displayed
on console screen
cout <<endl << "Roll number assigned to the student is : " << s1.roll_no; //
roll_no is displayed on the screen
} // main function ends
55
OOPs Lab Manual
Execution phase:
Task 3:
Write a C++ program to print the average of three numbers entered by the
user by creating a class named 'Average' having a function to calculate and
print the average without creating any object of the Average class.
Source code:
56
OOPs Lab Manual
/*****************/
int a;
int b;
int c;
int sum;
/****************/ // variable declaration
main()
{ // main function starts
Average a1; // object created with instance (a1)
a1.avg(); // average method called
} // main function ends
Execution phase:
57
OOPs Lab Manual
LAB EXPERIMENT # 8
Example:
58
OOPs Lab Manual
#include <iostream>
using namespace std;
class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor
private:
double length;
};
return 0;
}
Output:
Phase 1:
59
OOPs Lab Manual
Phase 2:
60
OOPs Lab Manual
#include <iostream>
class constructorDemo{
public:
int num;
char ch;
*/
constructorDemo() {
};
int main(){
* class_name object_name;
*/
constructorDemo obj;
61
OOPs Lab Manual
*/
cout<<"num: "<<obj.num<<endl;
cout<<"ch: "<<obj.ch;
return 0;
}
Output:
num: 100
ch: A
Program TASK 1: Solution - You have to do up to n = 15
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
for(int i = 1; i<10; i++)
{
int N, zeroth=3, first=2, second;
cout << "Enter in an integer N: " << flush;
cin >> N;
if(N==0)
cout << "u(" << N << ") = " << zeroth << endl << endl;
else if (N==1)
cout << "u(" << N << ") = " << first << endl << endl;
else if (N>1)
{
for(int x = 2; x<=N; x++)
{
second = x*first + (x+1)*zeroth + x;
zeroth = first;
62
OOPs Lab Manual
first = second;
}
cout << "u(" << N << ") = " << second << endl << endl;
}
}
cin.get();
}
Output:
Enter in an integer N: 1
u(1) = 2
Enter in an integer N: 2
u(2) = 15
Enter in an integer N: 3
u(3) = 56
Enter in an integer N: 4
u(4) = 303
For n=15
63
OOPs Lab Manual
Output part1:
64
OOPs Lab Manual
Output part 2:
65
OOPs Lab Manual
Task 2:
Source code:
#include<iostream>
class Car{
66
OOPs Lab Manual
public:
string brand;
string model;
int year;
brand=x;
model=y;
year=z;
cout <<endl << x << "\t" << y << "\t "<< z <<"\t";
};
main()
Car("BMW","X5",1999);
Car("FORD","Mustang",1969);
}
67
OOPs Lab Manual
Execution:
Task 3:
Source code:
#include<iostream>
class Car {
public:
string brand;
string model;
68
OOPs Lab Manual
int year;
};
brand=x;
model=y;
year=z;
main()
Car carObj1("BMW","X5",1999);
Car carObj2("FORD","Mustang",1969);
cout << carObj1.brand << " " << carObj1.model << carObj1.year <<"\n" ;
cout << carObj2.brand << " " << carObj2.model << carObj2.year <<"\n" ;
69
OOPs Lab Manual
Execution:
70
OOPs Lab Manual
LAB EXPERIMENT # 9
Objective: To implement inheritance concept using class and subclass and display
the output.
Example 1:
#include <iostream>
using namespace std;
class stud {
protected:
int roll, m1, m2;
public:
void get()
{
cout << "Enter the Roll No.: "; cin >> roll;
cout << "Enter the two highest marks: "; cin >> m1 >> m2;
}
};
class extracurriculam {
protected:
int xm;
public:
void getsm()
{
cout << "\nEnter the mark for Extra Curriculam Activities: "; cin >> xm;
}
};
class output : public stud, public extracurriculam {
int tot, avg;
public:
void display()
{
tot = (m1 + m2 + xm);
avg = tot / 3;
cout << "\n\n\tRoll No : " << roll << "\n\tTotal : " << tot;
cout << "\n\tAverage : " << avg;
}
};
int main()
{
71
OOPs Lab Manual
output O;
O.get();
O.getsm();
O.display();
}
Output:
First part:
Second part:
72
OOPs Lab Manual
Example 2:
#include <iostream>
using namespace std;
class Person
{
public:
string profession;
int age;
int main()
{
MathsTeacher teacher;
73
OOPs Lab Manual
teacher.profession = "Teacher";
teacher.age = 23;
teacher.display();
teacher.teachMaths();
Footballer footballer;
footballer.profession = "Footballer";
footballer.age = 19;
footballer.display();
footballer.playFootball();
return 0;
}
Output:
First part:
Second part:
74
OOPs Lab Manual
Task 1:
Write a C++ program to make a class named Fruit with a data member to calculate
the number of fruits in a basket. Create two other class named Apples and Mangoes
to calculate the number of apples and mangoes in the basket. Print the number of
fruits of each type and the total number of fruits in the basket.
Task 2:
Write a C++ program to calculate the total marks of each student of a class in
Physics, Chemistry and Mathematics and the average marks of the class. The user
enters the number of students in the class. Create a class named Marks with data
members for roll number, name and marks. Create three other classes inheriting
the Marks class, namely Physics, Chemistry and Mathematics, which are used to
define marks in individual subject of each student. Roll number of each student will
be generated automatically.
Task 3:
75
OOPs Lab Manual
Write a program with a mother class and an inherited daughter class. Both of them
should have a method void display() that prints a message (different for mother and
daughter).In the main define a daughter and call the display() method on it.
LAB EXPERIMENT # 10
76
OOPs Lab Manual
Example 1:
#include<iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i =0) {real = r; imag = i;}
// This is automatically called when '+' is used with
// between two Complex objects
Complex operator + (Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2; // An example call to "operator+"
c3.print();
}
Output:
Example 2:
// pointers to base class
#include <iostream>
using namespace std;
class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
77
OOPs Lab Manual
};
int main () {
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = ▭
Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << rect.area() << '\n';
cout << trgl.area() << '\n';
return 0;
}
Output:
Task 1:
Create one example of your own.
LAB EXPERIMENT # 11
78
OOPs Lab Manual
Example 1:
// exceptions
#include <iostream>
using namespace std;
int main () {
try
{
throw 20;
}
catch (int e)
{
cout << "An exception occurred. Exception Nr. " << e << '\n';
}
return 0;
}
Output:
Task 1:
Write a C++ program to create a class with its own operator new. This
operator should allocate ten objects, and on the eleventh object run out of
memory and throw an exception. Also add a static member function that
reclaims this memory. Now create a main( ) with a try block and
a catch clause that calls the memory-restoration routine. Put these inside
a while loop, to demonstrate recovering from an exception and continuing
execution.
LAB EXPERIMENT # 12
79
OOPs Lab Manual
Objective: To implement the concept of collection in java and run the program
Example:
package MyPack;
class Balance {
String name;
double bal;
Balance(String n, double b) {
name = n;
bal = b;
}
void show() {
if(bal<0)
System.out.print("--> ");
System.out.println(name + ": $" + bal);
}
}
class AccountBalance {
public static void main(String args[]) {
Balance current[] = new Balance[3];
current[0] = new Balance("K. J. Fielding", 123.23);
current[1] = new Balance("Will Tell", 157.02);
current[2] = new Balance("Tom Jackson", -12.33);
for(int i=0; i<3; i++) current[i].show();
}
}
Output:
80
OOPs Lab Manual
LAB EXPERIMENT # 13
81
OOPs Lab Manual
import java.applet.Applet;
import java.awt.Graphics;
Using a text editor, create a file named Hello.html in the same directory that
contains HelloWorld.class. This HTML file should contain the following text:
<HTML>
<HEAD>
<TITLE> A Simple Program </TITLE>
</HEAD>
<BODY>
Output:
82
OOPs Lab Manual
83