All Assgnments (1,2,3) Oop
All Assgnments (1,2,3) Oop
void displayDetails() {
cout << "Brand: " << brand << endl; cout <<
"Model: " << model << endl; cout << "Serial
Number: " << serial << endl; cout << "Colour:
" << colour << endl; cout << "Price: $" <<
price << endl;
cout << "Processor Speed: " << processorSpeed << " GHz" << endl;
cout << "RAM: " << RAM << " GB" << endl; cout << "Screen Size: " <<
screenSize << " inches" << endl;
}
};
int main() { Laptop
myLaptop;
myLaptop.displayDetails();
return 0;
}
2) Design a class named Rectangle to represent a rectangle. The class
contains:
• Two private double data fields named side1 and side2 that specify the two
sides of the rectangle.
• Public set and get methods for each of the two data fields.
• A public method named getArea() that returns the area of a rectangle.
• A public method named getPerimeter() that returns the perimeter
#include <iostream>
#include <string> using
namespace std;
public:
Rectangle(double w, double h) : width(w), height(h) {}
double getArea() {
return width * height;
}
double getPerimeter() {
return 2 * (width + height);
}
};
int main() {
Rectangle myRectangle(5.0, 3.0);
cout << "Rectangle area: " << myRectangle.getArea() << endl; cout <<
"Rectangle perimeter: " << myRectangle.getPerimeter() <<
endl;
return 0;
}
3) Write a class called rectangle. Your task is to store the length and width of
the rectangle. Write a member function called increment that will add 1 to
the value of length and width. Also write a function that will compute the
area of the rectangle.
#include <iostream>
#include <string> using
namespace std;
public:
Rectangle(double l, double w) : length(l), width(w) {}
void increment() {
length++; width++;
}
double computeArea() {
return length * width;
}
};
int main() {
Rectangle myRectangle(5.0, 3.0);
myRectangle.increment();
cout << "After incrementing length and width by 1, area: " <<
myRectangle.computeArea() << endl;
return 0;
}
4) Design a class named Course. The class contains:
• A private data field named courseName of type String.
• A private data field named students of type String[]. The size of the array is
100.
• A private data field named numberOfStudents of type int.
• Three get methods for data fields courseName, students[] and
numberOfStudents.
• A method named addStudent(String) that adds a student to the Course.
• A method named dropStudent(String) that drops a student from the
Course.
• A method named clear() that removes all students from the Course.
#include <iostream>
#include <string> using
namespace std;
public:
Course(const string& name) : courseName(name), numStudents(0) {} const
std::string& getCourseName() const {
return courseName;
}
void addStudent(const string& student) { if
(numStudents < MAX_STUDENTS) {
students[numStudents] = student; numStudents++;
} else { cout << "Course is full. Cannot add more students." <<
endl;
}
}
void clear() {
numStudents = 0;
}
};
int main() {
Course mathCourse("Mathematics");
mathCourse.addStudent("Alice"); mathCourse.addStudent("Bob");
mathCourse.addStudent("Charlie");
cout << "Number of students in " << mathCourse.getCourseName() << ": "
<< mathCourse.getNumStudents() << endl;
mathCourse.dropStudent("Bob");
#include <iostream>
Class Rectangle (
Double width;
Width++
myRectangle.incrementi):
return 0; 1
i
i
Assgnment( 2)
#include <iostream>
#include <string>
Class Laptop (
Public:
String brand;
String model;
Int serial;
String colour,
Float price:
Float processorSpeed,
Int RAM:
Float screenSize,
Void displayDetails() {
int main() {
Laptop myLaptop,
myLaptop.brand = "Dell";
myLaptop.model = "Inspiron";
myLaptop.serial = 12345;
myLaptop.colour = "Silver";
myLaptop.price = 999.99,
myLaptop.processorSpeed 2.4;
myLaptop RAM = 8;
myLaptop.screenSize 15.6;
myLaptop displayDetails();
return 0;
2) Design a class named
Rectangle to represent a
rectangle. The class contains:
#include <iostream>
#include <string>
Class Rectangle (
Private:
Double width;
Double height;
Return 0;
) Design a class named Course. The class
contains:
• A private data field named
courseName of type String.
#include <iostream>
#include <string>
Class Course {
Private:
String students[MAX_STUDENTS];
Int numStudents;
Public:
Return courseName:
numStudents++;
} else {
Int main() {
Course mathCourse(“Mathematics”);
mathCourse.addStudent(“Alice”);
mathCourse.addStudent(“Bob”);
mathCourse.addStudent(“Charlie”);
mathCourse.dropStudent(“Bob”);
mathCourse.clear();
return 0;
}
Assgnment 3
1- Perform the following tasks:
- Write another
main()function that
instantiates an array
of 10 TestClass
objects. Save the file
as TestClassArray.cpp.
Run this program and
observe the results.
In test class
#include <iostream>
Class TestClass
Private:
Int privateInt:
Public:
#include <iostream>
Class TestClass
Private:
Int privateInt:
Public
TestClass() {
Int main() {
TestClass objArray[10]:
return 0:
}
#include <iostream>
Class Date (
Private:
Int month:
Int day,
Int year,
Void displayDate() (
Int main() {
Date defaultDate;
defaultDate.displayDate();
customDatte.displayDate();
return 0;
}
3-Create a Person class that includes
fields for last name, first name, and zip
code. Include
#include <iostream>
#include <string>
Class Person
Private:
String lastName
String firstName:
String zipCode:
Public:
Persons): lastName(“X”),
firstName(“X”), zip Code(“X”){
Void displayPerson) (
Int main() {
}
Person defaultPerson;
defaultPerson.displayPerson();
return. 0;
}
4-Create a class named SavingsAccount.
Provide fields for the customer (use the
Person class from Exercise 3), account
number, balance, and interest rate.
Provide two constructors. One requires
a customer and sets each numeric field
to 0; the other requires a customer and
an account number and sets the
account’s opening balance to $100 at
3% interest. Include a function that
displays an account’s data fields. Write
a main()function that instantiates one
Savings Account object of each type
and displays their values. Save the file
as SavingsAccount-588-
#include <iostream>
#include <string>
Void displayPerson() (
LTE
Public
Person customer,
Double balance
Double interestRate:
Void displayAccount() (
Cout<<”\nAccount 2
Information:”<<end
account2.displayAccount();
Return 0;
}
#include <iostream>
Car count++
Car) (
Count-
Count
Return 0;
Class City:
Class Customer (
Private:
Class Oty
Public:
Void displayCustomerCity(const
Customer customer, const City city)
( cout << “Customer Number: <c
customer.customerNumber << endl;
displayCustomerCity(customer, city):
retum 0;
}
7- Create two classes. The first, named
Sale, holds data for a sales transaction.
Its private data members include the
day of the month, amount of the sale,
and the salesperson’ ID number. The
second class, named Salesperson, holds
data for a salesperson, and its private
data members include each
salesperson’s ID number and last name.
Each class includes a constructor to
which you can pass the field values.
Create a friend function named
display()that is a friend of both classes
and displays the date of sale, the
amount, and the salesperson ID and
name. Write a short
main()demonstration program to test
your classes and friend function. Save
the file as Sales.cop.
Minclude ciostreamo
Dinclude catring
Class Salesperson,
Class Sale
Private:
Int dayOfMonth
Double amount
Int salespersonie
Class Salesperson (
Private:
Int salespersonid,
String lastName;
Salesperson(int I Name(name) ()
Int main() {
Sale sale (15, 250.75, 1);
Display(sale, salesperson);
Return 0;
}