0% found this document useful (0 votes)
21 views37 pages

All Assgnments (1,2,3) Oop

The document discusses four programming assignments involving classes in C++. The first assignment involves creating a Laptop class with data members for brand, model, etc. and display functions. The second creates a Rectangle class with methods for area and perimeter. The third adds increment functions to the Rectangle class. The fourth creates a Course class to track students with add/drop functions.

Uploaded by

m.3mokhtar3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views37 pages

All Assgnments (1,2,3) Oop

The document discusses four programming assignments involving classes in C++. The first assignment involves creating a Laptop class with data members for brand, model, etc. and display functions. The second creates a Rectangle class with methods for area and perimeter. The third adds increment functions to the Rectangle class. The fourth creates a Course class to track students with add/drop functions.

Uploaded by

m.3mokhtar3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Assgnment (1)

Moamen Mohsen Sayed


ID : 222200084
Field: CSE
Program: AIS
1) Write a program in C++ that creates a class called laptop. The data
members of the class are brand (string), model (string), serial (int), colour
(string), price (float), processor speed (float), RAM (int), screen size(float).
Create member function that will set the individual values. Since the RAM can
be upgraded therefore create a function that allows you to upgrade the RAM
only. In the end, create a function that will display all the data members.
#include <iostream>
#include <string> using
namespace std; class
Laptop { public:
string brand; string
model; int serial; string
colour; float price; float
processorSpeed; int
RAM; float screenSize;

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.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:
• 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;

class Rectangle { private:


double width; double
height;

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;

class Rectangle { private:


double length; double
width;

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);

cout << "Initial area: " << myRectangle.computeArea() << endl;

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.

Write a test class to test your methods.

#include <iostream>
#include <string> using
namespace std;

class Course { private:


string courseName; static const int
MAX_STUDENTS = 100; string
students[MAX_STUDENTS]; int
numStudents;

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 dropStudent(const string& student) {


for (int i = 0; i < numStudents; i++) {
if (students[i] == student) {
for (int j = i; j < numStudents - 1; j++) {
students[j] = students[j + 1];
}
numStudents--;
return;
}
}
cout << student << " is not enrolled in this course." << std::endl;
}
int getNumStudents() const {
return numStudents;
}

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");

cout << "Number of students after dropping Bob: " <<


mathCourse.getNumStudents() << endl;

mathCourse.clear(); cout << "Number of students after


clearing the course: " << mathCourse.getNumStudents() << endl;
return 0;
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;

Class Rectangle (

Private: double length;

Double width;

Public: Rectangle(double I, double w): length(1), width(w) ()

Void increment() ( length++;

Width++

Double computeArea() { return length width:

Int main() ( Rectangle myRectangle(5.0, 3.0);

Cout << “Initial area: “ << myRectangle.computeArea() << endl;

myRectangle.incrementi):

cout << “After incrementing length and width by 1, area: “ <<


myRectangle.computeAreal) << endl;

return 0; 1
i
i

Assgnment( 2)

1) Write a program in C++ that


creates a class called laptop.
The data members of the class
are brand (string), model
(string), serial (int), colour
(string), price (float), processor
speed (float), RAM (int), screen
size(float). Create member
function that will set the
individual values. Since the
RAM can be upgraded
therefore create a function
that allows you to upgrade the
RAM only. In the end, create a
function that will display all
the data members.

#include <iostream>

#include <string>

Using namespace std;

Class Laptop (

Public:

String brand;

String model;

Int serial;

String colour,

Float price:

Float processorSpeed,

Int RAM:

Float screenSize,

Void displayDetails() {

Cout << “Brand: “ << brand << endl;


Cout << “Model: “ << model << endl;

Cout << "Serial Number: " << serial <<


endl;

cout << "Colour: " << colour << endl;

cout << "Price: 5" << price << endl;

cout << "Processor Speed: " <<


processorSpeed << GHz" << endl;

cout << "RAM: " << RAM <<"GB" <<


endl;

cout << "Screen Size: " << screenSize


<<" inches" << endl

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:

• 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;

Class Rectangle (

Private:

Double width;

Double height;

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.getAreal) << endl;

Cout << “Rectangle perimeter: “<<


myRectangle.getPerimeter() << endl;

Return 0;
) 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.

Write a test class to test your methods.

#include <iostream>

#include <string>

Using namespace std;

Class Course {

Private:

String course Name:

Static const int MAX_STUDENTS = 100;

String students[MAX_STUDENTS];

Int numStudents;

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;

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”);

cout << “Number of students after


dropping Bob: “ <<
mathCourse.getNumStudents() << endl;

mathCourse.clear();

cout << “Number of students after


clearing the course: “ <<
mathCourse.getNumStudents() << endl;

return 0;
}
Assgnment 3
1- Perform the following tasks:

- Create a class named


TestClass that holds a
single private integer
field and a public
constructor. The only
statement in the
constructor is one that
displays the message
“Constructing”. Write
a main()function that
instantiates one
object of the
TestClass. Save the file
as TestClass.cpp. Run
the program and
observe the results.

- 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:

TestClass() { std::cout << “Constructing”


<< std::endl;

Int main(){ TestClass obj: return 0;

In test class array

#include <iostream>

Class TestClass

Private:
Int privateInt:

Public

TestClass() {

Std::cout << “Constructing” <<


std::endl;

Int main() {

TestClass objArray[10]:

return 0:
}

2- Write the class definition for a


Date class that contains three
integer data members: month,
day, and year. Include a default
constructor that assigns the
date 1/1/2000 to any new
object that does not receive
arguments. Also include a
function that displays the Date
object. Write a main()function
in which you instantiate two
Date objects-one that you
create using the default
constructor values, and one
that you create using three
arguments-and display its
values. Save the file as
Date.cpp.

#include <iostream>

Using namespace std;

Class Date (
Private:

Int month:

Int day,

Int year,

Public Date(): month(1), day(1),


year(2000) (
}

Date(int m, int d, int y): month(m),


day(d), year(y) [

Void displayDate() (

Cout month <<<< day << “” << year <<


endl;

Int main() {

Date defaultDate;

Date customDate(12, 25, 2022),

Cout << “Default Date:”:

defaultDate.displayDate();

cout << “Custom Date:”;

customDatte.displayDate();

return 0;
}
3-Create a Person class that includes
fields for last name, first name, and zip
code. Include

A default constructor that initializes last


name, first name, and zip code to “X” if
no arguments are supplied. Also include
a display function. Write a main()
function that instantiates and displays
two Person objects: one that uses the
default values, and one for which you
supply your own values. Save the file as
Person.cpp.

#include <iostream>

#include <string>

Using namespace std;

Class Person

Private:

String lastName

String firstName:

String zipCode:
Public:

Persons): lastName(“X”),
firstName(“X”), zip Code(“X”){

Person/const string last, const string &


first, const strings zip): lastName last),
firstName(first), zipCode(zip) [
}

Void displayPerson) (

Cout << “Last Name: lastName <<


std::endl;

Cout << “First Name: << firstName <<


std::endl

Cout << “Zip Code:”<< zipCode <<


std::endl;

Int main() {
}

Person defaultPerson;

Person customPerson(“Doe”, “John”,


“12345”
Cout << “Default Person: << endl;

defaultPerson.displayPerson();

cout<<”\nCustom Person” << endl;


customPerson.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>

Using namespace std; class Person


( public: string last Name; string
firstName, string zipCode;

Person: lastName(“X”), firstName(“X”),


zipCode(“”)()

Person/const stringil last, const string&


first, const string& zip) lastName[last),
firstname(first), zipCode(zip) ()

Void displayPerson() (

Cout << “Last Name: “ << lastName <<


endl

Cout << “First Name:”<< firstName <<


endl;

Cout << “Zip Code:”<<zipCode << endl;»


}
};

LTE

Class Savings Account

Public

Person customer,

Int account Number;

Double balance

Double interestRate:

Savings Account(): account Number(0),


balance(0), interest Rate(0) []

SavingsAccount(const Person& cust, int


accNum): customer/cust),
accountNumber[accNumi balance(100),
interest Rate(0.03) ()

Void displayAccount() (

Cout << “Customer Informations” andl;


customer displayPerson

Cout << “Account Number:”<< account


Number << end

Cout << “Balance: 5 balance << endl;

Cout << “Interest Rate: interestitate 100


c “ << endl;
}
};

Int main() ( Person customer1[“Doe”,


“John”, “12345”)
Savings Account account2;

Cout << “Account 1 information: <<


endl; account1.displayAccount();

Cout<<”\nAccount 2
Information:”<<end
account2.displayAccount();

Return 0;
}

5-Create a class named Car. The Car


class contains a static integer field
named count. Create a constructor that
adds 1 to the count and displays the
count each time a Car is created. Create
a destructor that subtracts 1 from the
count and displays the count each time
a Car goes out of scope. Write a main()
function that declares an array of five
Car objects. Output consists of five
constructor messages and five
destructor messages, each displaying
the current count, similar to the output
in Figure 8- 34. Save the file as Car.cpp.

#include <iostream>

Using namespace std;

Class Car ( public:

Static int count


Car()}{

Car count++

Cout << “Car created. Count: count <<


endl;

Car) (

Count-

Cout << “Car destroyed. Count: ec count


<< endl;

Count

Int main() ( Cat cars[5]

Return 0;

6-Create two classes. The first holds


customer data-specifically. A customer
number and zip code. The second, a
class for cities, holds the city name,
state, and zip code. Additionally, each
class contains a constructor that takes
parameters to set the field values.
Create a friend function that displays a
customer number and the customer’s
city, state, and zip code. Write a brief
main()function to test the classes and
friend function. Save the file as
Customer.cpp.

#inclode #include <string> using


namespace std;

Class City:

Class Customer (

Private:

Int customerNumber, string zipCode;

Public Customer(int number, const std


string zip): customerNumber(number),
zipCode(zip) ()

Friend void displayCustomerCity(const


Customers customer, const Cityl cityl

Class Oty

Private: string cityflame; string state,


string zipCode:

Public:

City(const std::string& tstringst, const


string zip cityName(name), state(st),
zipCode(zip) ()

Friend void displayCustomer City(const


Cust const City& city);

Void displayCustomerCity(const
Customer customer, const City city)
( cout << “Customer Number: <c
customer.customerNumber << endl;

Cout << “City:”<< city.cityflame State:


ec city state <<”, Zip Code:”<<
city.zipCode << endl;

Int main() ( Customer customer(12345,


“12345”);

City city(“New York”, “NY”, “12345”);

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.

Add a function to both the Sale and


Salesperson classes that returns the
private salesperson ID number. Write a
main()function that contains an array of
five Salesperson objects and store
appropriate data in it. Then, continue to
prompt the user for Sale data until the
user enters an appropriate sentinel
value. For each Sale transaction
entered, determine whether the
salesperson’s ID number is valid. Either
display an error message, or use the
friend display()function to display all
the data. Save the file as cpp,

Minclude ciostreamo

Dinclude catring

Using namespace stal

Class Salesperson,

Class Sale

Private:

Int dayOfMonth

Double amount

Int salespersonie

Salețint day, double ant, int id):


dayOfMonthiday), amount(amt),
salespersoold(id) ()

Friend void display(const Sale sale,


const Salesperson salesperson);

Class Salesperson (

Private:

Int salespersonid,

String lastName;

Salesperson(int I Name(name) ()

Friend void display(ronst

Void display(const Sale sale, const


Salespersonă salesperson) {

“Date of Sale” ec sale dayOfMonth ec


endl;

“Amount of Sale 5 sale amount

Satesperson il last name <<endl;


}

Int main() {
Sale sale (15, 250.75, 1);

Salesperson salesperson(1, “Smith”);

Display(sale, salesperson);

Return 0;
}

You might also like