0% found this document useful (0 votes)
24 views

Abdullah Farooq OOP Assign No.4

Uploaded by

hifadi1393
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)
24 views

Abdullah Farooq OOP Assign No.4

Uploaded by

hifadi1393
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/ 22

OOP Assignment No.

4
Submitted to: Dr Nadeem Ashraf

.
F2023266160
ABDULLAH FAROOQ
OOP Assignment No.4

Question 1
Task 1:
#include <iostream>
using namespace std;
class A
{
public:
A()
{
cout << "I am constructor of class A." <<endl;
}
~A()
{
cout << "I am destructor of class A." <<endl;
}
};
class B : public A
{
public:
B()
{
cout << "I am constructor of class B." <<endl;
}
~B()
{
cout << "I am destructor of class B." <<endl;
}
};
class C : public B
{
public:
C()
{
cout << "I am constructor of class C." <<endl;
}
~C()
{
cout << "I am destructor of class C." <<endl;
}
};
class D : public C
{
public:
D()
{
cout << "I am constructor of class D." <<endl;
}
~D()
{
cout << "I am destructor of class D." <<endl;
}
};

int main()
{
D obj;
return 0;
}

Task 2:
#include <iostream>
using namespace std;
class A
{
private:
int a;
public:
A()
{
cout << "I am default constructor of class A." <<endl;
}
A(int x)
{
a = x;
cout << "I am parametriized constructor of class A.The value of a = " << a <<endl;
}
};
class B : public A
{
private:
int b;
public:
B()
{
cout << "I am default constructor of class B." <<endl;
}
B(int x, int y) : A(x)
{
b = y;
cout << "I am parametriized constructor of class B.The value of b = " << b <<endl;
}
};
class C : public B
{
private:
int c;
public:
C()
{
cout << "I am default constructor of class C." <<endl;
}
C(int x, int y, int z) : B(x, y)
{
c = z;
cout << "I am parametriized constructor of class C.The value of c = " << c <<endl;
}
};
class D : public C
{
private:
int d;
public:
D()
{
cout << "I am default constructor of class D." <<endl;
}
D(int x, int y, int z, int j) : C(x, y, z)
{
d = j;
cout << "I am parametriized constructor of class D.The value of d = " << d <<endl;
}
};

int main()
{
D obj;
D obj2(1, 2, 3, 4);
return 0;
}

Task 3:
a.
#include <iostream>
using namespace std;

class Person
{
private:
string name;
public:
Person()
{
name = "Abdullah";
}
void show()
{
cout << "Name:" << name <<endl;
}
};
class Student : public Person
{
private:
int rollNo;
public:
Student()
{
rollNo = 160;
}
void show()
{
Person::show();
cout << "Roll no:" <<rollNo<<endl;
}
};

int main()
{
Student s1;
Student s2(s1);
cout << "The information of s1 object:" <<endl;
s1.show();
cout << "The information of s2 object after initializing it with already existing object:"
<<endl;
s2.show();
return 0;
}

Task 3:
b.
#include <iostream>
using namespace std;

class Person
{
private:
string name;
public:
Person()
{
name = "Abdullah";
}
void show()
{
cout << "Name:" << name <<endl;
}
Person(const Person& obj) // copy constructor
{
name = obj.name;
}
};
class Student : public Person
{
private:
int rollNo;
public:
Student()
{
rollNo = 160;
}
void show()
{
Person::show();
cout << "Roll no:" <<rollNo<<endl;
}
};

int main()
{
Student s1;
Student s2(s1);
cout << "The information of s1 object:" <<endl;
s1.show();
cout << "The information of s2 object after initializing it with already existing object:"
<<endl;
s2.show();
return 0;
}

Task 3:
c.
#include <iostream>
using namespace std;

class Person
{
private:
string name;
public:
Person()
{
name = "Abdullah";
}
void show()
{
cout << "Name:" << name <<endl;
}
Person(const Person& obj) // copy constructor
{
name = obj.name;
}
};
class Student : public Person
{
private:
int rollNo;
public:
Student()
{
rollNo = 160;
}
void show()
{
Person::show();
cout << "Roll no:" <<rollNo<<endl;
}
Student(const Student& obj) // copy constructor
{
rollNo = obj.rollNo;
}
};

int main()
{
Student s1;
Student s2(s1);
cout << "The information of s1 object:" <<endl;
s1.show();
cout << "The information of s2 object after initializing it with already existing object:"
<<endl;
s2.show();
return 0;
}

Task 4:
#include <iostream>
using namespace std;
class Person
{
private:
float weight, height;
string gender;
public:
Person(float w, float h, string gen)
{
weight = w;
height = h;
gender = gen;
}
void display()
{
cout << "Weight:" << weight <<endl;
cout << "Height:" << height <<endl;
cout << "Gender:" << gender <<endl;
}
};
class Employee
{
private:
string designation;
int hoursPerDay;
public:
Employee(string d, int hrs)
{
designation = d;
hoursPerDay = hrs;
}
void display()
{
cout << "Designation:" << designation <<endl;
cout << "Hours Per Day:" <<hoursPerDay<<endl;
}
};

class Teacher : public Person, public Employee


{
public:
Teacher(float w, float h, string gen, string d, int hrs):Person(w,h,gen),Employee(d, hrs)
{
}
void display()
{
cout << "Details related to teacher are:" <<endl;
Person::display();
Employee::display();
}
};

int main()
{
Teacher t1(70.00, 5.8, "Male", "Lecturer", 8);
t1.display();
return 0;
}

Task 5:
#include <iostream>
using namespace std;
class Person
{
float weight, height;
string gender;
public:
Person(float w, float h, string g)
{
weight = w;
height = h;
gender = g;
}
void walk()
{
cout << "I can Walk." <<endl;
}
void sit()
{
cout << "I can Sit." <<endl;
}
void show()
{
walk();
sit();
cout << "Weight:" << weight <<endl;
cout << "Height:" << height <<endl;
cout << "Gender:" << gender <<endl;
}
};

class Student:public Person


{
int ID;
string firstName, lastName, Graduation;
public:
Student(float w, float h, string g, string fname, string lname, string grad):Person(w, h, g)
{
firstName = fname;
lastName = lname;
Graduation = grad;
}
void write()
{
cout << "I can write." <<endl;
}
void printDetail()
{
show();
write();
cout << "First Name:" <<firstName<<endl;
cout << "Last Name:" <<lastName<<endl;
cout << "Graduation:" << Graduation <<endl;
}
};

class GraduationStudent : public Student


{
string UniversityName;
int yearGraduation;
public:
GraduationStudent(float w, float h, string g, string fname, string lname, string grad, string
uName, int year):Student(w,h,g,fname,lname,grad)
{
UniversityName = uName;
yearGraduation = year;
}
void display()
{
printDetail();
cout << "University:" <<UniversityName<<endl;
cout << "Year of Graduation" <<yearGraduation<<endl;
}
};

int main()
{
GraduationStudentgs(62.00, 5.6, "Male", "Abdul", "Rafay", "CS", "UMT", 2027 );
gs.display();
return 0;
}
Task 6:
#include <iostream>
using namespace std;

class Date
{
int day, month, year;
public:
Date(int d, int m, int y)
{
day = d;
month = m;
year = y;
}
void display()
{
cout << "Date:" << day <<endl;
cout << "Month:" << month <<endl;
cout << "Year:" << year <<endl;
}
};

class Time
{
int hours, min, sec;
public:
Time(int hrs, int mi, int s)
{
hours = hrs;
min = mi;
sec = s;

}
void display()
{
cout << "Hours:" << hours <<endl;
cout << "Minutes:" << min <<endl;
cout << "Seconds:" << sec <<endl;
}
};
class Time_Date : public Date, public Time
{
public:
Time_Date(int d, int m, int y, int hrs, int mi, int s):Date(d,m,y),Time(hrs,m,s)
{
}
void display()
{
Date::display();
Time::display();
}
};

int main()
{
Time_Datetd(6,12,2024,8,30,20);
td.display();
return 0;
}

Task 7:
#include <iostream>
using namespace std;

class Shape
{
protected:
float height, width;
public:
Shape(float h, float w)
{
height = h;
width = w;
}
};

class Color
{
protected:
string colorName;
public:
Color(string color)
{
colorName = color;
}
};

class Rectangle : public Shape, public Color


{
float area;
public:
Rectangle(float h, float w, string c): Shape(h,w), Color(c)
{
}
float calculateArea()
{
return height * width;
}
void showcolor()
{
cout << "Color:" <<colorName<<endl;
}
};
int main()
{
Rectangle r(5.6, 2.9, "blue");
float area = r.calculateArea();
cout << "Area:" << area <<endl;
r.showcolor();
return 0;
}

Question 2
Function Templates:
A function template allows you to create a function that can operate with any data type. You
define the function template once, and then you can use it with different data types without
repeating the function definition.
Syntax:
template <typename T>
T functionName(T arg1, T arg2)
{
// function implementation
}
template <typename T>: This line tells the compiler that the function definition is a template
with a type parameter T.
T functionName(T arg1, T arg2): This is the function prototype where T is used as a placeholder
for the data type.

Class Templates:
A class template allows you to define a class that can operate with any data type. Like function
templates, you define the class template once, and then you can use it with different data types
without repeating the class definition.
Syntax:
template <typename T>
class ClassName
{
public:
T memberVariable;
ClassName(T arg) : memberVariable(arg) {}
T memberFunction(T arg);
};

template <typename T>


T ClassName<T>::memberFunction(T arg) {
// function implementation
return arg;
}
template <typename T>: This line tells the compiler that the class definition is a template with a
type parameter T.
ClassName<T>: This specifies the template type parameter T in the class definition.
Examples:
1.
#include <iostream>
using namespace std;

template <typename T>


void swapValues(T &a, T &b)
{
T temp = a;
a = b;
b = temp;
}
int main()
{
int x = 5, y = 10;
swapValues(x, y);
cout << x << " " << y <<endl;
return 0;
}

2.
#include <iostream>
using namespace std;

template <typename T>


T findMax(T a, T b)
{
return (a > b) ? a : b;
}
int main() {
cout <<findMax(3, 7) <<endl;
return 0;
}
3.
#include <iostream>
using namespace std;

template <typename T1, typename T2>


class Pair
{
public:
T1 first;
T2 second;
Pair(T1 f, T2 s) : first(f), second(s) {}
void display() {
cout << first << ", " << second <<endl;
}
};

int main() {
Pair<int, double> p1(1, 2.5);
p1.display();
return 0;
}

4.
#include <iostream>
using namespace std;

template <typename T>


void sortArray(T arr[], int size)
{
for (int i = 0; i< size - 1; i++)
for (int j = i + 1; j < size; j++)
if (arr[i] >arr[j])
swap(arr[i], arr[j]);
}

int main() {
int arr[] = {5, 3, 8, 1};
int size = sizeof(arr) / sizeof(arr[0]);
sortArray(arr, size);
for (int i :arr) cout <<i<< " ";
return 0;
}

5.
#include <iostream>
using namespace std;

template <typename T>


class Box
{
private:
T content;
public:
Box(T c) : content(c) {}
void setContent(T c) {
content = c;
}
T getContent() const {
return content;
}
void display() const {
cout << "Box content: " << content <<endl;
}
};

int main() {
Box<int>intBox(123);
intBox.display();

Box<string>stringBox("Hello, World!");
stringBox.display();

return 0;
}

Question 3
Operator overloading:
Operator overloading in C++ allows you to redefine the way operators work for user-defined
types, making the language highly extendable. By overloading operators, you can provide
intuitive syntax for custom operations on objects, making your code more readable and
expressive.

#include <iostream>
using namespace std;
class Distance
{
private:
int feet;
int inches;
void convert()
{
feet += inches / 12;
inches = inches % 12;
}
public:
Distance(int f = 0, int i = 0)
{
feet = f;
inches = i;
convert();
}
Distance operator+(const Distance &d) // Overload + operator
{
Distance temp;
temp.feet = feet + d.feet;
temp.inches = inches + d.inches;
return temp;
}
Distance operator-(const Distance &d) // Overload - operator
{
Distance temp;
temp.feet = feet - d.feet;
temp.inches = inches - d.inches;
return temp;
}
Distance operator*(const Distance &d) // Overload * operator
{
Distance temp;
temp.feet = feet * d.feet;
temp.inches = inches * d.inches;
return temp;
}
Distance operator/(const Distance &d) // Overload / operator
{
Distance temp;
temp.feet = feet / d.feet;
temp.inches = inches / d.inches;
return temp;
}
Distance operator%(const Distance &d) // Overload % operator
{
Distance temp;
temp.feet = feet % d.feet;
temp.inches = inches % d.inches;
return temp;
}

friend ostream& operator<<(ostream&output, const Distance &d) // Overload << operator


for output
{
output <<d.feet<< " feet " <<d.inches<< " inches";
return output;
}

friend istream& operator>>(istream&input, Distance &d) // Overload >> operator for input
{
input >>d.feet>>d.inches;
d.convert();
return input;
}
};

int main() {
Distance d1(5, 8);
Distance d2(3, 4);

Distance d3 = d1 + d2;
cout << "d1 + d2 = " << d3 <<endl;

Distance d4 = d1 - d2;
cout << "d1 - d2 = " << d4 <<endl;

Distance d5 = d1 * d2;
cout << "d1 * d2 = " << d5 <<endl;

Distance d6 = d1 / d2;
cout << "d1 / d2 = " << d6 <<endl;

Distance d7 = d1 % d2;
cout << "d1 % d2 = " << d7 <<endl;

Distance d8;
cout << "Enter distance (feet inches): ";
cin>> d8;
cout << "You entered: " << d8 <<endl;

return 0;
}

----------------------------------------------------------------------------------------------------

You might also like