Os Manual
Os Manual
Os Manual
LAB MANUAL
BACHELOR OF TECHNOLOGY
IN
Mathematics and Computing
SUBMITTED BY :
Aditya Harsh (2K20/MC/12)
Objective : The aim is to make students learn how to create classes and their objects in
an object oriented program. Students also learn how the member function can be defined
within a class and how an object can call the member function of its class from the main
function with an dot operator.
Procedure/ Methodology :
1. Create a class “Time” with data members and member functions as written in the aim
of the practical.
2. Create two objects of the class “Time” inside the main function using the syntax:
“Time obj1, obj2”.
3. Set the data members for each of the object with the help of dot operator, i.e., call the
member function, say obj1.setTime, and set the data members for both the objects.
4. Call the print function from the object(s) defined inside the main function with the help
of dot operator to print its hour, minute and seconds.
CODE:
#include <iostream>
class Time {
private:
int hour;
int minute;
int seconds;
public:
// Member function to set the time
void setTime(int h, int m, int s) {
hour = h;
minute = m;
seconds = s;
}
int main() {
// Create two objects of the Time class
Time obj1, obj2;
return 0;
}
OUTPUT:
Experiment 2
Aim: Write a program in C++ demonstrating the use of constructor (with no
arguments), constructor (with parameterized arguments) and destructor. Create objects
of that class in main and test those objects with these constructors and destructor
function.
Objective : The aim is to make students learn how to create constructors and destructor
functions inside a class. Students also learn how any object of the class allocates its
memory and de-allocates its memory after calling the constructor and destructor function
respectively.
Procedure/ Methodology :
1. Create any class in C++ with some data members, member functions and a
constructor (with parameters and without parameters) and a destructor
function.
2. Call the constructor function to set the data members for the object from the
main. Use the dot operator to call the constructor.
3. Call any member function to verify the values for the data members set by
the constructor.
4. Call the destructor to de-allocate the memory reserved by the object of the
class. Use the dot operator to call the destructor.
CODE:
#include <iostream>
class MyClass {
private:
int value;
public:
// Constructor with no arguments
MyClass() {
std::cout << "Constructor with no arguments called." << std::endl;
value = 0;
}
// Destructor
~MyClass() {
std::cout << "Destructor called." << std::endl;
}
void display() {
std::cout << "Value: " << value << std::endl;
}
};
int main() {
// Create objects of the class
MyClass obj1; // Constructor with no arguments called
MyClass obj2(42); // Parameterized constructor called
Objective : The aim is to make students learn how operator overloading works in
object oriented programming. Students also learn two methods of operator
overloading: one with friend function, and other without friend function.
Software/ Resources used : C++ programming language
Procedure/ Methodology :
1. Create any class in C++ with some data members, member functions and
some operator functions that are to be overloaded.
2. Create one friend function of the class. Use this friend function to overload
binary and unary operators.
3. Create objects of that class inside main function. Call the operator functions
(friend functions and non-friend member functions) with the dot operator
from the objects of that class.
4. Observe the behavior of the overloaded functions and analyze how those
operators can be customized to work with objects of any class.
CODE:
#include <iostream>
class MyClass {
private:
int value;
public:
MyClass(int val) : value(val) {}
void display() {
std::cout << "Value: " << value << std::endl;
}
int main() {
MyClass obj1(10);
MyClass obj2(5);
return 0;
}
Experiment 4
Aim: Implement inheritance in C++ program. Create a base class named Person (with
data members first_name, and last_name). Derive a class named Employee from base
class Person. The Employee class should include a data member named employee_id.
Further a class named Manager should be derived from the class Employee and the
Manager class should include two data members named employee_designation and
employee_salary. Create at least three objects of class Manager, specify the values of all
their attributes, and determine which manager has the highest salary.
Objective: The aim is to make students learn how inheritance works in C++. Students
also learn how to create child classes from the parent class, relationship between the child
and parent class and how objects of child class interact with parent class.
Procedure/ Methodology :
1. Create any class in C++ with some data members and member functions.
2. Create a child class from the above defined parent class using public inheritance, i.e.,
use public keyword while inheriting the parent class.
3. For the child class, inherit the public members from the parent class, and create some
extra data members specific to the child class which are not present in the parent class.
4. Create objects of both parent and child classes inside the main function.
5. For both types of objects, call their corresponding member functions from the main
function with the dot operator.
CODE:
#include <iostream>
#include <string>
class Person {
protected:
std::string first_name;
std::string last_name;
public:
Person(const std::string& first, const std::string& last)
: first_name(first), last_name(last) {}
void display() {
std::cout << "Name: " << first_name << " " << last_name <<
std::endl;
}
};
public:
Employee(const std::string& first, const std::string& last, int id)
: Person(first, last), employee_id(id) {}
void display() {
Person::display();
std::cout << "Employee ID: " << employee_id << std::endl;
}
};
public:
Manager(const std::string& first, const std::string& last, int id,
const std::string& designation, double salary)
: Employee(first, last, id), employee_designation(designation),
employee_salary(salary) {}
void display() {
Employee::display();
std::cout << "Designation: " << employee_designation << std::endl;
std::cout << "Salary: $" << employee_salary << std::endl;
}
double getSalary() {
return employee_salary;
}
};
int main() {
Manager manager1("John", "Doe", 101, "Senior Manager", 75000.0);
Manager manager2("Jane", "Smith", 102, "Assistant Manager", 60000.0);
Manager manager3("Bob", "Johnson", 103, "Manager", 80000.0);
return 0;
}
Experiment 5
Objective : The aim is to make students learn the concept of templates in object
oriented programming, i.e. with general class templates, customized classes of integers,
floats, or any other data type can be created at run-time.
Procedure/ Methodology :
1. Create a general class template <T> in C++ with some data members and
member functions of type <T>.
2. Customize the class template <T> with integer, create an integer class at
run-time, and test the member functions of that class with integer objects
from the main function.
3. Customize the class template <T> with floats, create a float class at run-
time, and test the member functions of that class with float objects from the
main function.
4. Customize the class template <T> with character data type, create a
character type class at run-time, and test the member functions of that class
with character objects from the main function.
Conclusion: With this practical, students can learn the concept of class templates,
i.e., how general class templates <T> can be customized specifically to work with
integers, floats or any other data type at run time.
CODE:
#include <iostream>
#include <string>
public:
Pair(T1 val1, T2 val2) : first(val1), second(val2) {}
void display() {
std::cout << "Pair: " << first << ", " << second << std::endl;
}
};
int main() {
// Create a Pair for integers
Pair<int, int> intPair(10, 20);
std::cout << "Integer Pair:" << std::endl;
intPair.display();
return 0;
}
Experiment 6
Objective : The aim is to make students learn the concept of exception handling, and
how the exceptions can be handled at various levels, i.e., handling the exceptions at first
catch block, or rethrowing it and further catching it at next try - catch block.
Procedure/ Methodology :
1. Create any class with some data members and member functions.
2. Create a try-catch block with some parameters, say divide by zero or
overflow condition.
3. Throw an exception from the try block and catch that exception with its
corresponding catch block.
4. Then, throw a different exception from the try block, do not catch that
exception in the first catch block, rather rethrow the same exception from
its catch block so that it is handled by the next try – catch block, thus
implementing the concept of rethrowing an exception.
CODE:
#include <iostream>
#include <cassert>
class MyException {
public:
MyException(const char* message) : message_(message) {}
private:
const char* message_;
};
int main() {
try {
// Test case 1: Division by non-zero denominator
int result = divide(10, 2);
assert(result == 5);
std::cout << "Test case 1: Result = " << result << std::endl;
} catch (const MyException& e) {
std::cerr << "Test case 1: Caught an exception: " << e.what() <<
std::endl;
}
try {
// Test case 2: Division by zero
int result = divide(10, 0);
std::cout << "Test case 2: Result = " << result << std::endl;
} catch (const MyException& e) {
std::cerr << "Test case 2: Caught an exception: " << e.what() <<
std::endl;
} catch (...) {
std::cerr << "Test case 2: Caught an unknown exception." << std::endl;
}
return 0;
}
Experiment 7
Aim: Create an inheritance hierarchy (any inheritance example of your choice) in JAVA.
Create one super class and three sub classes should inherit from that super class. Also
highlight the concept of function overriding and function overloading in your program.
Objective: The aim is to make students learn the concept of super class and sub classes
in Java programming. Students also learn the difference between function overloading
and function overriding in object oriented programming.
Software/ Resources used : Java programming language
Procedure/ Methodology :
1. Create any class in Java with some data members and member functions.
2. Inherit three sub classes from the above defined super class with public
inheritance.
3. Create two functions with same name in the super class and implement function
overloading with difference in terms of number or type of arguments.
4. Create two functions with same name, one in super class and other in sub class,
and implement function overriding by calling the super class member function and
sub class member function from the sub class object defined within the main
function.
5. Test member functions of super and sub classes by creating objects of these classes
inside the main function.
CODE:
class Shape {
protected String name;
@Override
public void calculateArea() {
double area = Math.PI * radius * radius;
System.out.println("Area of " + name + " (Circle): " + area);
}
@Override
public void calculatePerimeter() {
double perimeter = 2 * Math.PI * radius;
System.out.println("Perimeter of " + name + " (Circle): " + perimeter);
}
}
@Override
public void calculateArea() {
double area = width * height;
System.out.println("Area of " + name + " (Rectangle): " + area);
}
@Override
public void calculatePerimeter() {
double perimeter = 2 * (width + height);
System.out.println("Perimeter of " + name + " (Rectangle): " + perimeter);
}
}
@Override
public void calculateArea() {
double area = 0.5 * base * height;
System.out.println("Area of " + name + " (Triangle): " + area);
}
circle.display();
circle.calculateArea();
circle.calculatePerimeter();
rectangle.display();
rectangle.calculateArea();
rectangle.calculatePerimeter();
triangle.display();
triangle.calculateArea();
triangle.calculatePerimeter(3.0, 4.0, 5.0); // Overloaded method with
different arguments
}
}
OUTPUT:
EXPERIMENT 8
Aim: Write a JAVA program explaining the working of constructors along with the
super keyword.
Objective : The aim is to make students learn the concept of constructors in Java
programming. Students also learn the concept of Super keyword in Java, i.e., how super
class constructor and methods can be called from the sub class with Super keyword.
1. Create any class in Java with some data members and member functions.
2. Inherit a sub class from the above defined super class with public inheritance.
3. Create constructor for both sub class and super class.
4. Call the constructor of the super class from the sub class constructor.
5. Now, apply the Super keyword and then call the constructor of the super class from
the sub class constructor and observe the differences.
6. Test member functions of super and sub classes by creating objects of these classes
inside the main function.
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
@Override
public double calculateArea() {
return width * height;
}
}
Aim: Write a Java program consisting of abstract classes and abstract method(s). The
first concrete sub class in the inheritance hierarchy must implement the abstract
method(s). Create the objects of the concrete class and test your program.
Objective : The aim is to make students learn the concept of abstract classes and
abstract methods in Java. Students also learn the difference between concrete class and
abstract class in object oriented programming and how abstract methods of abstract
classes can be overridden in non-abstract sub classes.
Software/ Resources used : Java programming language
Procedure/ Methodology :
1. Create any abstract class in Java with some data members and abstract member
functions.
2. Inherit two sub classes from the above defined super class with public inheritance,
one with abstract keyword and other without abstract keyword.
3. Override the abstract method of the super class in the non abstract sub class.
4. Create objects of non abstract classes and call their corresponding methods.
5. Try to create objects of abstract classes and analyze the difference between abstract
classes and concrete classes.
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
@Override
public double calculateArea() {
return width * height;
}
}
Aim: Write a Java program explaining the working of static methods in a class.
Objective : The aim is to make students learn the concept of static and non-static
methods and data members. Students learn how one copy of static data member is shared
by all objects of a class as compared to non static members, which every object has its
own copy of.
Procedure/ Methodology :
1. Create any class in Java with some data members and member functions.
2. Create some members and methods as static and some as non-static.
3. Define the body of static method with static data members.
4. Call the static and non-static member functions from the objects of the class
created inside the main function.
5. Change the value of static data member from different objects and observe how
one copy of static data member is shared by all objects of the class.
class MyClass {
// Static data member shared by all objects
static int staticVariable = 0;
// Non-static method
void setNonStaticVariable(int value) {
nonStaticVariable = value;
}
OUTPUT:
EXPERIMENT 11
1. Create any class in Java that inherits Applet in built class and implements the
interface of Action Listener.
2. Create some text fields, labels, buttons and JLabel in the class.
3. Set their corresponding names and labels for those text fields and buttons.
4. Set their foreground and background colors, if required.
5. Run the Java program and observe the output in Java enabled web browser.