Download Skill Base Lab - OOPM (C++ and Java) Experiment 1: Implementing a Prime Number Program and more Assignments Computer science in PDF only on Docsity!
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Name of Student:
Class/DIV: Semester:
Roll Number:
Academic Year: 2023-
Subject: Skill base Lab ā OOPM: (C++ and Java)
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Name of Student: Roll Number : Class/DIV : Academic Year :2023- EXPT 1: Implement a program that prints prime numbers till a given number Date of Performance: Date of Submission: Program Execution (04) Documentation (02) Timely Submission (02) Viva Answer (02) Experiment Marks (10) Teacher Signature
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
#include
bool isPrime(int num) {
if (num <= 1)
return false;
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0)
return false;
return true;
int main() {
int n;
std::cout << "Enter a number: ";
std::cin >> n;
std::vector primeNumbers;
for (int i = 2; i <= n; ++i) {
if (isPrime(i)) {
primeNumbers.push_back(i);
std::cout << i << " ";
std::cout << "\nPrime numbers up to " << n << " have been stored in the array." <<
std::endl;
return 0;
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Output:
Enter a number: 20
Prime numbers up to 20 have been stored in the array.
Conclusion:
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Experiment Number: 2
Implement a program to create a class and give values to TWO object attributes of that class. Aim: Write a program to make two classes: Driver and Vehicle Class driver must have attributes: Name, Age, Salary, Class Vehicle must have attributes: Model, Plate_No, Capacity, and Driver Name. Make two objects of each class Theory: C++ has the following conditional statements: ā Use if to specify a block of code to be executed, if a specified condition is true ā Use else to specify a block of code to be executed, if the same condition is false (Write answers of questions:
- Explain with example what are Classes and Objects in C++?
- Object-oriented programming has several advantages over procedural programming: Comment. ) Flowchart / Algorithm:
- Define the Driver Class: ļ· Define a class called Driver with the following public attributes: ļ· Name (string): To store the name of the driver. ļ· Age (int): To store the age of the driver. ļ· Salary (double): To store the salary of the driver.
- Define the Vehicle Class: ļ· Define a class called Vehicle with the following public attributes: ļ· Model (string): To store the model of the vehicle. ļ· Plate_No (string): To store the license plate number of the vehicle. ļ· Capacity (int): To store the seating capacity of the vehicle. ļ· DriverName (string): To store the name of the driver assigned to the vehicle.
- Main Function: ļ· In the main function, create two instances of the Driver class named driver1 and driver2. ļ· Assign values to the attributes of driver1 and driver2: ļ· Set Name, Age, and Salary for each driver. ļ· Create two instances of the Vehicle class named vehicle1 and vehicle2. ļ· Assign values to the attributes of vehicle1 and vehicle2: ļ· Set Model, Plate_No, Capacity, and DriverName for each vehicle. ļ· Print the information for each driver and vehicle using the cout statements: ļ· Print the driver's name, age, and salary for both drivers.
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
ļ· Print the vehicle's model, license plate number, seating capacity, and assigned driver's name for both vehicles.
- End of Program: ļ· Return 0 from the main function to indicate successful execution of the program. Program: #include #include class Driver { public: std::string Name; int Age; double Salary; }; class Vehicle { public: std::string Model; std::string Plate_No; int Capacity; std::string DriverName; }; int main() { Driver driver1, driver2; driver1.Name = "Taranjeet Singh"; driver1.Age = 30; driver1.Salary = 50000.0;
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Vehicle 1: Model: Toyota Camry, Plate No: ABC123, Capacity: 5, Driver: Taranjeet Singh Vehicle 2: Model: Honda Civic, Plate No: XYZ789, Capacity: 4, Driver: Rohit Shah Conclusion:
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Name of Student: Roll Number : Class/DIV : Academic Year :2022 2023 EXPT 3: Implement a program to perform abstraction on certain class members. Write a method to perform encapsulation to access the abstract class members Date of Performance: Date of Submission: Program Execution (04) Documentation (02) Timely Submission (02) Viva Answer (02) Experiment Marks (10) Teacher Signature
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
ļ· Use the setter methods to set the private attributes of driver1:
ļ· Set Name, Age, and Salary.
ļ· Use the getter methods to retrieve the private attributes of driver1:
ļ· Retrieve and print the driver's name, age, and salary using cout
statements.
5. End of Program:
ļ· Return 0 from the main function to indicate successful execution of the program.
Program:
#include
#include
class Driver {
private:
std::string Name;
int Age;
double Salary;
public:
// Setter methods
void setName(const std::string& newName) {
Name = newName;
void setAge(int newAge) {
Age = newAge;
void setSalary(double newSalary) {
Salary = newSalary;
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
// Getter methods
std::string getName() const {
return Name;
int getAge() const {
return Age;
double getSalary() const {
return Salary;
int main() {
Driver driver1;
// Using the set methods to set the private attributes
driver1.setName("Taranjeet Singh");
driver1.setAge(30);
driver1.setSalary(50000.0);
// Using the get methods to retrieve the private attributes
std::cout << "Driver 1: " << driver1.getName() << ", Age: " << driver1.getAge() << ",
Salary: $" << driver1.getSalary() << std::endl;
return 0;
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Name of Student: Roll Number :
Class/DIV : Academic Year :2023-
EXPT 4: Implement a program to perform operator overloading on unary and Binary
operators
Date of Performance: Date of Submission:
Program Execution (04) Documentation (02) Timely Submission (02) Viva Answer (02) Experiment Marks (10) Teacher Signature
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Experiment Number: 4
Implement a program to perform operator overloading on unary and Binary operators Aim: a. Write a program to add two complex numbers with operator overloading, the real and imaginary part must be added separately and output must be of the form a + ib b. Write a program to apply distance negation by overloading the ā-ā operator Theory: Things to Remember in C++ Operator Overloading
- Two operators = and & are already overloaded by default in C++. For example, to copy objects of the same class, we can directly use the = operator. We do not need to create an operator function.
- Operator overloading cannot change the precedence and associativity of operators. However, if we want to change the order of evaluation, parentheses should be used.
- There are 4 operators that cannot be overloaded in C++. They are: :: (scope resolution) . (member selection) .* (member selection through pointer to function) ?: (ternary operator) (Write answers of the questions: How to overload increment/decrement operator? What is C++ Binary Operator Overloading/ what is C++ Unary Operator Overloading) Flowchart / Algorithm: (A) -
- Define the Complex Class: ļ· Define a class called Complex. ļ· Declare private attributes inside the class: ļ· real (double): To store the real part of the complex number. ļ· imaginary (double): To store the imaginary part of the complex number.
- Define Constructor: ļ· Define a constructor for the Complex class that takes two parameters: ļ· r (double): Initialize the real attribute with the provided value. ļ· i (double): Initialize the imaginary attribute with the provided value.
- Define Addition Operator Overloading: ļ· Define an operator overloading function operator+ that takes a const Complex& other
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
ļ· Return the newly created Distance object.
- Define Display Function: ļ· Define a display() function: ļ· Print the feet value followed by the word "feet", a space, the inches value, and the word "inches".
- Main Function: ļ· In the main function: ļ· Create a Distance object named original with initial feet and inches values. ļ· Use the negation operator to create a new Distance object named negated by negating the original distance. ļ· Use the display() function to print both the original and negated distances.
- End of Program: ļ· Return 0 from the main function to indicate successful execution of the program. Program: (A) - #include class Complex { private: double real; double imaginary; public: Complex(double r = 0.0, double i = 0.0) : real(r), imaginary(i) {} // Operator overloading for addition Complex operator+(const Complex& other) const { return Complex(real + other.real, imaginary + other.imaginary); } // Function to display the complex number void display() const { std::cout << real << " + " << imaginary << "i";
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
int main() { Complex num1(3.0, 4.0); Complex num2(1.5, 2.5); Complex sum = num1 + num2; std::cout << "Complex Number 1: "; num1.display(); std::cout << std::endl; std::cout << "Complex Number 2: "; num2.display(); std::cout << std::endl; std::cout << "Sum: "; sum.display(); std::cout << std::endl; return 0; } (B)- #include class Distance { private: int feet; int inches; public: Distance(int ft = 0, int in = 0) : feet(ft), inches(in) {}