Nothing Special   »   [go: up one dir, main page]

Assignment

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 29

#include <iostream>

#include <stdexcept>

#include <string>

using namespace std;

class dateType {

public:

dateType(int month = 1, int day = 1, int year = 1900);

void setDate(int month, int day, int year);

void printDate() const;

private:

int dMonth;

int dDay;

int dYear;

bool isLeapYear(int year) const;

};

dateType::dateType(int month, int day, int year) {

setDate(month, day, year);

void dateType::setDate(int month, int day, int year) {

if (year < 1900 || month < 1 || month > 12 || day < 1 || day > 31) {

throw invalid_argument("Invalid date");

if (month == 2) {

if (isLeapYear(year)) {

if (day > 29) {


throw invalid_argument("Invalid date");

} else {

if (day > 28) {

throw invalid_argument("Invalid date");

} else if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) {

throw invalid_argument("Invalid date");

dMonth = month;

dDay = day;

dYear = year;

void dateType::printDate() const {

cout << dMonth << "/" << dDay << "/" << dYear;

bool dateType::isLeapYear(int year) const {

return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));

class personType {

public:

personType(string first = "", string last = "");

void setName(string first, string last);

string getFirstName() const;


string getLastName() const;

private:

string firstName;

string lastName;

};

personType::personType(string first, string last) {

setName(first, last);

void personType::setName(string first, string last) {

firstName = first;

lastName = last;

string personType::getFirstName() const {

return firstName;

string personType::getLastName() const {

return lastName;

int main() {

try {

dateType myDate(2, 30, 2024); // Leap year

myDate.printDate();

cout << std::endl;

personType myPerson("jawad", "malik");


cout << "Name: " << myPerson.getFirstName() << " " << myPerson.getLastName() << endl;

} catch (const invalid_argument& e) {

cerr << "Error: " << e.what() <<endl;

#include <iostream>

#include <stdexcept>

#include <string>

using namespace std;

class dateType {

public:

dateType(int month = 1, int day = 1, int year = 1900);

void setDate(int month, int day, int year);


void printDate() const;

bool isLeapYear(int year) const;

private:

int dMonth;

int dDay;

int dYear;

};

dateType::dateType(int month, int day, int year) {

setDate(month, day, year);

void dateType::setDate(int month, int day, int year) {

if (year < 1900 || month < 1 || month > 12) {

throw invalid_argument("Invalid month or year");

if (month == 2) {

if (isLeapYear(year)) {

if (day < 1 || day > 29) {

throw invalid_argument("Invalid day for February in a leap year");

} else {

if (day < 1 || day > 28) {

throw invalid_argument("Invalid day for February");

} else if ((month == 4 || month == 6 || month == 9 || month == 11) && (day < 1 || day > 30)) {
throw invalid_argument("Invalid day for the specified month");

} else if ((day < 1 || day > 31)) {

throw invalid_argument("Invalid day");

dMonth = month;

dDay = day;

dYear = year;

void dateType::printDate() const {

cout << dMonth << "/" << dDay << "/" << dYear;

bool dateType::isLeapYear(int year) const {

return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));

int main() {

try {

dateType myDate(2, 29, 2024); // Leap year

myDate.printDate();

cout << endl;

dateType invalidDate;

invalidDate.setDate(13, 32, 2023); // Invalid date

invalidDate.printDate();

} catch (const invalid_argument& e) {

cerr << "Error: " << e.what() <<endl;

}
}

Question 2;

#include <iostream>

using namespace std;

class DateType{

public:

int day, month, year;

DateType(int d, int m, int y) : day(d), month(m), year(y) {}

void disDate() const {

cout << day << "/" << month << "/" << year;

};

class PersonType {
public:

string firstName, lastName;

PersonType(const string& fName, const string& lName)

: firstName(fName), lastName(lName) {}

void disName() const {

cout << firstName << " " << lastName;

};

class DoctorType : public PersonType {

public:

string specialty;

DoctorType(const string& fName, const string& lName, const string& spec)

: PersonType(fName, lName), specialty(spec) {}

void disDoctorInfo() const {

disName();

cout<<endl;

cout << "Specialty: " << specialty << endl;

};

class BillType {

public:

int patientId;

double pharmCharges, docfee, roomCharge;


BillType(int id, double pharmacy, double fee, double room)

: patientId(id), pharmCharges(pharmacy), docfee(fee), roomCharge(room) {}

double totalCharges() const {

return pharmCharges + docfee + roomCharge;

};

class PatientType : public PersonType {

public:

int patientid, age;

DateType dOfBirth, adDate, disDate;

DoctorType attendPhysician;

PatientType(const string& fName, const string& lName, int id, int age,

const DateType& dob, const DateType& admit, const DateType& discharge,

const DoctorType& physician)

: PersonType(fName, lName), patientid(id), age(age),

dOfBirth(dob), adDate(admit), disDate(discharge),

attendPhysician(physician) {}

void disPatientInfo() const {

disName();

cout << " Patient ID: " << patientid <<endl;

cout<< " Age: " << age << endl;

cout << " DOB: ";

dOfBirth.disDate();

cout<<endl;

cout << " Admit Date: ";


adDate.disDate();

cout<<endl;

cout << " Discharge Date: ";

disDate.disDate();

cout << endl;

cout << "Attending Physician: ";

attendPhysician.disDoctorInfo();

};

int main() {

DoctorType doctor("jawad ", "younas", "fitness");

BillType bill(123, 500.0, 200.0, 20000.0);

PatientType patient("bhola", "abc", 123, 20,

DateType(10, 5, 1990), DateType(1, 1, 2024), DateType(10, 1, 2024), doctor);

cout << "Doctor Information:" << endl;

doctor.disDoctorInfo();

cout << "\nBill Information:" << endl;

cout << "Total Charges: " << bill.totalCharges() << endl;

cout << "\nPatient Information:" << endl;

patient.disPatientInfo();

return 0;
}

Question 3;

#include<iostream>

#include<cmath>

using namespace std;

class mathematics{

public:

double mean;

double standard_deviation;

double n1,n2,n3,n4,n5;

mathematics(double a,double b,double c,double d,double e)

n1=a;

n2=b;

n3=c;
n4=d;

n5=e;

double Calmean()

mean=(n1+n2+n3+n4+n5)/5;

return mean;

};

class A : protected mathematics{

public:

A(double n1, double n2, double n3, double n4, double n5)

: mathematics(n1, n2, n3, n4, n5) {}

double Calmean()

mean=(n1+n2+n3+n4+n5)/5;

return mean;

};

class B : private mathematics{

public:

B(double n1, double n2, double n3, double n4, double n5)
: mathematics(n1, n2, n3, n4, n5) {}

double CalStandardDeviation()

double meana = Calmean();

standard_deviation = sqrt(

((n1 - meana) * (n1 - meana) + (n2 - meana) * (n2 - meana) +

(n3 - meana) * (n3 - meana) + (n4 - meana) * (n4 - meana) +

(n5 - meana) * (n5 - meana)) / 5);

return standard_deviation;

};

int main()

double n1,n2,n3,n4,n5;

cout<<"Enter 5 numbers "<<endl;

cin>>n1>>n2>>n3>>n4>>n5;

A xobj(n1,n2,n3,n4,n5);

cout<<xobj.Calmean()<<endl;

B yobj(n1,n2,n3,n4,n5);

cout<<yobj.CalStandardDeviation();
}

Question 4;

#include<iostream>

using namespace std;

class dayType{

public:

string
week[7]={"sunday","monday","tuesday","wednesday","thursday","friday","sat
urday"};

string day;

int add;

string afteradd;

string currentday;

dayType(string a,int b)
{

day=a;

add=b;

void printday()

for(int i=0;i<7;i++)

if(day==week[i])

currentday=week[i];

cout<<"Selected Day is = "<<currentday<<endl;

void retday() {

int currentdayindex = -1;

for (int i = 0; i < 7; i++) {

if (day == week[i]) {

currentdayindex = i;

break;

}
}

if (currentdayindex != -1) {

int newdayindex = (currentdayindex + add) % 7;

afteradd = week[newdayindex];

cout << "After adding " << add << " days, day is " << afteradd << endl;

} else {

cout << "Invalid day input." << endl;

void nextday()

for(int i=0;i<7;i++)

if(day==week[i])

if(week[i]==week[6])

cout<<"Next day is "<<"sunday"<<endl;

else

cout<<"Next day is "<<week[i+1]<<endl;


}

void prevday()

for(int i=0;i<7;i++)

if(day==week[i])

if(week[i]==week[0])

cout<<"Previous day is "<<"saturday"<<endl;

else

cout<<"Previous day is "<<week[i-1]<<endl;

};
int main()

string day;

int a;

cout<<"Enter a day "<<endl;

cin>>day;

cout<<"Enter a day you want to add "<<endl;

cin>>a;

dayType obj(day,a);

obj.printday();

obj.nextday();

obj.prevday();

obj.retday();

}
Q 4;

#include <iostream>

using namespace std;

class fractionType {

private:

int numer;

int denom;

public:

fractionType(int num = 0, int den = 1) : numer(num), denom(den) {

if (denom == 0) {

cout << "Error: Denominator cannot be zero. Setting denominator to 1."


<< endl;
denom = 1;

fractionType operator+(const fractionType& other) const {

int newNumerator = numer * other.denom + other.numer * denom;

int newDenominator = denom * other.denom;

return fractionType(newNumerator, newDenominator);

fractionType operator-(const fractionType& other) const {

int newNumerator = numer * other.denom - other.numer * denom;

int newDenominator = denom * other.denom;

return fractionType(newNumerator, newDenominator);

fractionType operator*(const fractionType& other) const {

int newNumerator = numer * other.numer;

int newDenominator = denom * other.denom;

return fractionType(newNumerator, newDenominator);

fractionType operator/(const fractionType& other) const {


if (other.numer == 0) {

cout << "Error: Division by zero is undefined. Returning original fraction."


<< endl;

return *this;

int newNumerator = numer * other.denom;

int newDenominator = denom * other.numer;

return fractionType(newNumerator, newDenominator);

bool operator<(const fractionType& other) const {

return (numer * other.denom < other.numer * denom);

bool operator>(const fractionType& other) const {

return (numer * other.denom > other.numer * denom);

void display() const {

cout << numer << "/" << denom<<endl;

};

int main() {

fractionType fraction1(8, 16);


fractionType fraction2(4, 8);

fractionType resultAdd = fraction1 + fraction2;

fractionType resultSub = fraction1 - fraction2;

fractionType resultMul = fraction1 * fraction2;

fractionType resultDiv = fraction1 / fraction2;

bool isLessThan = fraction1 < fraction2;

bool isGreaterThan = fraction1 > fraction2;

cout << "Addition: ";

resultAdd.display();

cout << endl;

cout << "Subtraction: ";

resultSub.display();

cout << endl;

cout << "Multiplication: ";

resultMul.display();

cout << endl;


cout << "Division: ";

resultDiv.display();

cout << endl;

fraction1.display();

fraction2.display();

cout << "Less than: " << boolalpha << isLessThan << endl;

cout << "Greater than: " << boolalpha << isGreaterThan << endl;

return 0;

Q 6;

#include<iostream>

using namespace std;


template <typename A>

void sortion(A var[], int s)

A temp;

for (int i = 0; i < (s - 1); i++)

for (int j = i + 1; j < s; j++)

if (var[j] < var[i])

temp = var[i];

var[i] = var[j];

var[j] = temp;

cout << "Array after selection sort:" << endl;

for (int i = 0; i < s; i++)

cout << var[i] << " ";

cout << endl;

int main()
{

int s = 5;

int intvar[s] = {3, 2, 3, 5, 7};

sortion(intvar, s);

cout << endl;

cout << "Selection sort for float array:" << endl;

float floatvar[s] = {1.8, 6.6, 5.1, 8.9, 4.1};

sortion(floatvar, s);

cout << endl;

cout << "Selection sort for double array:" << endl;

double doublevar[s] = {47.34, 70.45, 35.222, 600.52, 192.31};

sortion(doublevar, s);

cout << endl;

return 0;

}
#include <iostream>

using namespace std;

class Publication {

public:

string title;

int pub_Year;

Publication(string t, int y) : title(t), pub_Year(y) {}

string Rtitle() const {

return title;

int Ryear() const {

return pub_Year;

virtual void disDetails() const {


cout << "This is the base class" << endl;

virtual ~Publication() {}

};

class Book : public Publication {

public:

string author;

int genre;

Book(string t, int y, string a, int g) : Publication(t, y), author(a), genre(g) {}

void disDetails() const override {

cout << "Book Details:" << endl;

cout << "Title: " << title << endl;

cout << "Publication Year: " << pub_Year << endl;

cout << "Author: " << author << endl;

cout << "Genre: " << genre << endl;

~Book() {}

};

class Magazine : public Publication {

public:

int issueNo;

Magazine(string t, int y, int i) : Publication(t, y), issueNo(i) {}


void disDetails() const override {

cout << " Magazine Details:" << endl;

cout << "Title: " << title << endl;

cout << "Publication Year: " << pub_Year << endl;

cout << "Issue Number: " << issueNo << endl;

~Magazine() {}

};

int main() {

Publication* publication1 = new Book("vampires tales ", 2021, "jawad", 6);

Publication* publication2 = new Magazine("nnjjj", 2024, 6);

publication1->disDetails();

cout << endl;

publication2->disDetails();

delete publication1;

delete publication2;
}

You might also like