LAB 1, 2, 6 Solution (Fellow)
LAB 1, 2, 6 Solution (Fellow)
#include <iostream>
using namespace std;
int main()
{
cout << "This is my First Program" << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n1,n2,n3;
cout << "Enter Three Numbers: ";
cin >> n1 >> n2 >> n3;
cout << "The middle number is: " << n2 << endl;
return 0;
}
int main() {
string str;
cout << "Enter a string: ";
getline(cin, str);
int wordCount = 0;
int spaceCount = 0;
return 0;
}
#include <iostream>
bool is_palindrome(int x) {
if (x < 0) {
return false;
}
int original_x = x;
int reversed_x = 0;
while (original_x > 0) {
reversed_x = reversed_x * 10 + original_x % 10;
original_x /= 10;
}
return x == reversed_x;
}
int main() {
int num;
if (is_palindrome(num)) {
cout << num << " is a palindrome." << endl;
} else {
cout << num << " is not a palindrome." << endl;
}
return 0;
}
#include <iostream>
using namespace std;
class Geometry{
public:
float length;
float width;
Geometry(){
cout << "Enter length of shape: ";
cin >> length;
cout << "Enter Width of the shape: ";
cin >> width;
if (length == width){
cout << "\nShape is square.\n";
}
else{
cout << "Shape is rectangle.";
}
}
};
int main()
{
Geometry geo;
return 0;
}
CORRECTED CODE
#include <iostream>
#include <string>
using namespace std;
class Student{
public:
string name;
string rollno;
string subjects[5];
void display(){
cout << name << '\n';
cout << rollno << '\n';
for (size_t i = 0; i < 5; i++)
{
cout << subjects[i] << endl;
}
cout << endl;
}
};
int main(){
std2.name = std1.name;
std2.rollno = std1.rollno;
for (size_t i = 0; i < 5; i++)
{
std2.subjects[i] = std1.subjects[i];
}
std1.display();
std2.display();
return 0;
}
#include <iostream>
using namespace std;
class Car {
private:
int wheels;
int doors;
int currentSpeed;
public:
// Constructor to initialize data members
Car(int numWheels, int numDoors) {
wheels = numWheels;
doors = numDoors;
currentSpeed = 0; // Initial speed is 0
}
int main() {
// Create two car objects
Car ferrari(4, 2); // Ferrari with 4 wheels and 2 doors
Car mustang(4, 4); // Mustang with 4 wheels and 4 doors
// Accelerate Ferrari
ferrari.speedUp();
ferrari.speedUp();
ferrari.speedUp();
return 0;
}
LAB 2
Task 01: Creating A Program with Classes and Objects
Write a C++ program that uses classes and objects to calculate the area of a rectangle, circle, or square
based on the user's choice and the dimensions they provide.
#include <iostream>
using namespace std;
class Rectangle{
public:
float height, width, area;
Rectangle(){
cout << "Enter Width and Height of rectangle: ";
cin >> width >> height;
cout << "\nYou chose rectangle.\n";
cout << "The area of Rectangle is: " << (width * height);
}
};
class Circle{
public:
float radius;
Circle(){
cout << "Enter the radius of circle: ";
cin >> radius;
cout << "\nYou chose circle.\n";
cout << "Area of circle is: " << (3.14759 * radius * radius);
}
};
class Square{
public:
float side;
Square(){
cout << "Enter the length of side of square: ";
cin >> side;
cout << "\nYou chose square\n";
cout << "Area of square is: " << (side*side);
}
};
int main()
{
int choice;
cout << "Choose the shape to calculate the area:\n";
cout << "1. Rectangle\n2. Circle\n3. Square\n";
cin >> choice;
switch (choice)
{
case 1:{
Rectangle rect;
break;
}
case 2:{
Circle circle;
break;
}
case 3:{
Square square;
break;
}
default:{
cout << "Please Enter a valid choice.";
break;
}
}
return 0;
}
#include <iostream>
using namespace std;
class Rectangle{
public:
float height, width, area;
Rectangle(){
cout << "Enter Width and Height of rectangle: ";
cin >> width >> height;
cout << "\nYou chose rectangle.\n";
cout << "The area of Rectangle is: " << (width * height);
}
};
class Circle{
public:
float radius;
Circle(){
cout << "Enter the radius of circle: ";
cin >> radius;
cout << "\nYou chose circle.\n";
cout << "Area of circle is: " << (3.14759 * radius * radius);
}
};
class Square{
public:
float side;
Square(){
cout << "Enter the length of side of square: ";
cin >> side;
cout << "\nYou chose square\n";
cout << "Area of square is: " << (side*side);
}
};
int main()
{
int choice;
cout << "Choose the shape to calculate the area:\n";
cout << "1. Rectangle\n2. Circle\n3. Square\n";
cin >> choice;
switch (choice)
{
case 1:{
Rectangle rect;
break;
}
case 2:{
Circle circle;
break;
}
case 3:{
Square square;
break;
}
default:{
cout << "Please Enter a valid choice.";
break;
}
}
return 0;
}
#include <iostream>
using namespace std;
class Rectangle{
public:
float height, width, area;
Rectangle(){
cout << "Enter Width and Height of rectangle: ";
cin >> width >> height;
cout << "\nYou chose rectangle.\n";
cout << "The area of Rectangle is: " << (width * height);
}
};
class Circle{
public:
float radius;
Circle(){
cout << "Enter the radius of circle: ";
cin >> radius;
cout << "\nYou chose circle.\n";
cout << "Area of circle is: " << (3.14759 * radius * radius);
}
};
class Square{
public:
float side;
Square(){
cout << "Enter the length of side of square: ";
cin >> side;
cout << "\nYou chose square\n";
cout << "Area of square is: " << (side*side);
}
};
int main()
{
int choice;
cout << "Choose the shape to calculate the area:\n";
cout << "1. Rectangle\n2. Circle\n3. Square\n";
cin >> choice;
switch (choice)
{
case 1:{
Rectangle rect;
break;
}
case 2:{
Circle circle;
break;
}
case 3:{
Square square;
break;
}
default:{
cout << "Please Enter a valid choice.";
break;
}
}
return 0;
}
#include <iostream>
using namespace std;
class Rectangle{
public:
float height, width, area;
Rectangle(){
cout << "Enter Width and Height of rectangle: ";
cin >> width >> height;
cout << "\nYou chose rectangle.\n";
cout << "The area of Rectangle is: " << (width * height);
}
};
class Circle{
public:
float radius;
Circle(){
cout << "Enter the radius of circle: ";
cin >> radius;
cout << "\nYou chose circle.\n";
cout << "Area of circle is: " << (3.14759 * radius * radius);
}
};
class Square{
public:
float side;
Square(){
cout << "Enter the length of side of square: ";
cin >> side;
cout << "\nYou chose square\n";
cout << "Area of square is: " << (side*side);
}
};
int main()
{
int choice;
cout << "Choose the shape to calculate the area:\n";
cout << "1. Rectangle\n2. Circle\n3. Square\n";
cin >> choice;
switch (choice)
{
case 1:{
Rectangle rect;
break;
}
case 2:{
Circle circle;
break;
}
case 3:{
Square square;
break;
}
default:{
cout << "Please Enter a valid choice.";
break;
}
}
return 0;
}
#include <iostream>
using namespace std;
class Rectangle{
public:
float height, width, area;
Rectangle(){
cout << "Enter Width and Height of rectangle: ";
cin >> width >> height;
cout << "\nYou chose rectangle.\n";
cout << "The area of Rectangle is: " << (width * height);
}
};
class Circle{
public:
float radius;
Circle(){
cout << "Enter the radius of circle: ";
cin >> radius;
cout << "\nYou chose circle.\n";
cout << "Area of circle is: " << (3.14759 * radius * radius);
}
};
class Square{
public:
float side;
Square(){
cout << "Enter the length of side of square: ";
cin >> side;
cout << "\nYou chose square\n";
cout << "Area of square is: " << (side*side);
}
};
int main()
{
int choice;
cout << "Choose the shape to calculate the area:\n";
cout << "1. Rectangle\n2. Circle\n3. Square\n";
cin >> choice;
switch (choice)
{
case 1:{
Rectangle rect;
break;
}
case 2:{
Circle circle;
break;
}
case 3:{
Square square;
break;
}
default:{
cout << "Please Enter a valid choice.";
break;
}
}
return 0;
}
#include <iostream>
using namespace std;
class Rectangle{
public:
float height, width, area;
Rectangle(){
cout << "Enter Width and Height of rectangle: ";
cin >> width >> height;
cout << "\nYou chose rectangle.\n";
cout << "The area of Rectangle is: " << (width * height);
}
};
class Circle{
public:
float radius;
Circle(){
cout << "Enter the radius of circle: ";
cin >> radius;
cout << "\nYou chose circle.\n";
cout << "Area of circle is: " << (3.14759 * radius * radius);
}
};
class Square{
public:
float side;
Square(){
cout << "Enter the length of side of square: ";
cin >> side;
cout << "\nYou chose square\n";
cout << "Area of square is: " << (side*side);
}
};
int main()
{
int choice;
cout << "Choose the shape to calculate the area:\n";
cout << "1. Rectangle\n2. Circle\n3. Square\n";
cin >> choice;
switch (choice)
{
case 1:{
Rectangle rect;
break;
}
case 2:{
Circle circle;
break;
}
case 3:{
Square square;
break;
}
default:{
cout << "Please Enter a valid choice.";
break;
}
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class MotorVehicle {
private:
string make;
string fuelType;
int yearOfManufacture;
string color;
int engineCapacity;
public:
// Constructor to initialize data members
MotorVehicle(const string& make, const string& fuelType, int year, const
string& color, int capacity) {
this->make = make;
this->fuelType = fuelType;
yearOfManufacture = year;
this->color = color;
engineCapacity = capacity;
}
// Getter functions
string getMake() const {
return make;
}
// Setter functions
void setMake(const string& newMake) {
make = newMake;
}
int main() {
string make, fuelType, color;
int year, engineCapacity;
return 0;
}
Assume you have to write a program for a cricket game. There are two teams and each team has a
value for score and number of wickets. You are required to design a class Team that has relevant
member functions for setting the score and wicket’s value and for getting these values too.
(a). The score and wicket of both the teams should be taken as an input from the user in main(). The
main function should interact with the class Team to set and get values. Write a function in the class
Team to update the score and wickets of a particular team.
Expected output (console):
****Enter data for Team1****
Enter score: 24
Enter the number of wickets: 4
Team 1 has score: 24 and number of wickets: 4
****Enter data for Team2****
Enter score: 67
Enter the number of wickets: 9
Team 1 has score: 67 and number of wickets: 9
Main function (driver function) should do the following tasks:
//declare objects to interact with the class //take input from user
//call set methods of class
//call getter methods and print to the console
(b). Declare two more objects and initialize one of the objects using the default constructor and the
other using a parameterized constructor. You may send hardcoded values while calling the
parametrized constructor.
Expected output (console):
**** Team3 created (default)****
Team 3 has score: 0 and number of wickets: 10
**** Team4 created (parameterized)****
Team 4 has a score: 11 and number of wickets: 2
Main function (driver function) should do the following tasks:
//declare objects to interact with the class //call getter methods and print to the console
(c). In main(), write a logic that finds the team with 1) minimum score, 2) maximum score, and 3)
greater than 6 wickets. The main function should interact with the class Team to set and get values.
First, display the scores and wickets for all teams and then report 1,2, and 3.
Expected output (console):
Team 1 has score: 24 and the number of wickets: 4
Team 2 has score: 12 and the number of wickets: 9
Team 3 has score: 120 and the number of wickets: 3
Team 4 has score: 11 and number of wickets: 7 Team 3 has maximum score Team 4 has minimum
score Team 2 and 4 have greater than 6 wickets
Main function (driver function) should do the following tasks:
//continuing the existing program…
//display values for teams using getter methods //find maximum etc..as asked
#include <iostream>
class Team {
public:
// Default constructor with reasonable initial values
Team() : score(0), wickets(10) {}
private:
int score;
int wickets;
};
int main() {
Team team1, team2; // Objects using default constructor
std::cout << std::endl << "****Enter data for Team2****" << std::endl;
std::cout << "Enter score: ";
std::cin >> team2.score;
std::cout << "Enter the number of wickets: ";
std::cin >> team2.wickets;
std::cout << std::endl << "**** Team1 created (default) ****" <<
std::endl;
std::cout << "Team 1 has score: " << team1.getScore() << " and the number
of wickets: " << team1.getWickets() << std::endl;
std::cout << std::endl << "**** Team2 created (default) ****" <<
std::endl;
std::cout << "Team 2 has score: " << team2.getScore() << " and the number
of wickets: " << team2.getWickets() << std::endl;
std::cout << std::endl << "**** Team3 created (parameterized) ****" <<
std::endl;
std::cout << "Team 3 has a score: " << team3.getScore() << " and number of
wickets: " << team3.getWickets() << std::endl;
std::cout << std::endl << "** Team Scores and Wickets **" << std::endl;
return 0;
}
#include <iostream>
#include <cmath>
class Circle {
public:
// Default constructor with origin at (0, 0) and radius 0
Circle() : radius(0.0), originX(0.0), originY(0.0) {}
int main() {
// Create 4 circles with hardcoded values for part (b)
Circle circles[4];
circles[0] = Circle(5.0, 1.0, 2.0);
circles[1] = Circle(3.0, 1.0, 2.0); // Concentric with circle1
circles[2] = Circle(7.0, 4.0, 3.0);
circles[3] = Circle(10.0, 1.0, 2.0); // Concentric with circle1 and
circle2
if (!areConcentric) {
cout << "No concentric circles found." << endl;
}
return 0;
}
Task 4: Constructor Implementation
Consider a class named Job that has a deadline as a data member and relevant getter/setter
method(s). Assume you have to schedule two earliest jobs on the basis of their deadlines. That is, if
there are three jobs in the system with deadlines (deadline1, deadline2, and deadline3, respectively)
then the system should report the top two earliest jobs (with the smallest deadline value). You might
need to find the deadline with the smallest and second smallest value.
#include <iostream>
#include <vector>
#include <algorithm> // For sorting
class Job {
private:
int deadline;
public:
// Constructor to initialize the deadline
Job(int d) : deadline(d) {}
int main() {
// Create some sample jobs with deadlines
std::vector<Job> jobs;
jobs.push_back(Job(5)); // Job 1
jobs.push_back(Job(3)); // Job 2
jobs.push_back(Job(7)); // Job 3
return 0;
}
#include <iostream>
#include <string>
class StringFormatter {
public:
StringFormatter(const std::string& text) : text_(text) {}
private:
std::string text_;
};
int main() {
std::string longText = "The world is indeed full of peril and in it there
are many dark places. But still there is much that is fair. And though in all
lands, love is now mingled with grief, it still grows, perhaps, the greater.";
StringFormatter formatter(longText);
return 0;
}
LAB 6
Task 01 Part(a): Shallow Copy • Create a class
"Rectangle" with the following attributes:
o Length (int)
o Width (int)
• Implement a copy constructor for the "Rectangle" class. The copy constructor should create a new
object that is a copy of an existing "Rectangle" object.
• In the main function, create a "Rectangle" object and initialize it with a length and width of your
choice.
• Create a second "Rectangle" object that is a copy of the first object using the copy constructor.
• Verify that the copy constructor correctly created a new object with the same length and width as
the original object.
• Modify the length or width of the second rectangle and verify that the changes affect both the
Rectangles
#include <iostream>
class Rectangle {
public:
int length;
int width;
// Constructor
Rectangle(int l, int w) : length(l), width(w) {}
// Copy constructor
Rectangle(const Rectangle& other) : length(other.length),
width(other.width) {}
};
int main() {
// Create the original Rectangle
Rectangle originalRectangle(10, 5);
return 0;
}
#include <iostream>
class Rectangle {
public:
int length;
int width;
// Constructor
Rectangle(int l, int w) : length(l), width(w) {}
int main() {
// Create the original Rectangle
Rectangle originalRectangle(10, 5);
// Verify that the deep copy constructor correctly created a new object
std::cout << "Original Rectangle: ";
originalRectangle.display();
std::cout << "Copied Rectangle: ";
copiedRectangle.display();
#include <iostream>
#include <vector>
#include <string>
class Student {
public:
std::string name;
int rollNumber;
int age;
// Constructor
Student(const std::string& n, int roll, int a) : name(n),
rollNumber(roll), age(a) {}
// Copy constructor
Student(const Student& other) : name(other.name),
rollNumber(other.rollNumber), age(other.age) {}
int main() {
// Create a vector of Student objects
std::vector<Student> originalStudents;
originalStudents.push_back(Student("Alice", 101, 20));
originalStudents.push_back(Student("Bob", 102, 21));
originalStudents.push_back(Student("Charlie", 103, 19));
return 0;
}
#include <iostream>
class ComplexNumber {
private:
double real;
double imaginary;
public:
// Constructor
ComplexNumber(double r, double i) : real(r), imaginary(i) {}
// Getter methods
double getReal() const {
return real;
}
// Operator overloads
ComplexNumber operator+(const ComplexNumber& other) const {
return ComplexNumber(real + other.real, imaginary + other.imaginary);
}
int main() {
// Create complex numbers
ComplexNumber num1(3.0, 2.0);
ComplexNumber num2(1.5, 4.0);
// Display results
std::cout << "Complex Number 1: ";
num1.show();
std::cout << "Complex Number 2: ";
num2.show();
return 0;
}
#include <iostream>
#include <vector>
class Matrix {
private:
int row;
int col;
std::vector<std::vector<int>> mat;
public:
// Constructor
Matrix(int r, int c) : row(r), col(c) {
mat.resize(row, std::vector<int>(col, 0));
}
// Display matrix
void display() const {
for (const auto& rowVec : mat) {
for (int val : rowVec) {
std::cout << val << " ";
}
std::cout << std::endl;
}
}
int main() {
// Example usage
Matrix A(2, 3);
A.setElement(0, 0, 1);
A.setElement(0, 1, 2);
A.setElement(0, 2, 3);
A.setElement(1, 0, 4);
A.setElement(1, 1, 5);
A.setElement(1, 2, 6);
return 0;
}