Aleena Akram 22F-8801 Lab 6: Task 1
Aleena Akram 22F-8801 Lab 6: Task 1
22F-8801
LAB 6
TASK 1
#include<iostream>
using namespace std;
enum week { monday, tuesday, wednesday, thursday, friday, saturday, sunday };
int main()
{
week s;
s = monday;
cout << "yesteday = " << s - 1 << endl;
cout << "tomorrow = " << s+1 << endl;
system("pause");
}
TASK NO 2
#include<iostream>
using namespace std;
union aleena
{
int x = 65;
char y;
double z;
};
int main()
{
aleena s;
s.x = 65;
cout << "-----------------display size of union ------------------- " <<
sizeof(s) << endl;
cout << "display integer " << (s.x) << endl;
cout << "display char " << (s.y) << endl;
cout << "display double " << (s.z) << endl;
s.y = 'A';
cout << "---------------------display size of union ----------------------- "
<< sizeof(s) << endl;
cout << "display integer " << (s.x) << endl;
cout << "display char " << (s.y) << endl;
cout << "display double " << (s.z) << endl;
s.z = 67.5;
cout << "------------------------display size of union ---------------------- "
<< sizeof(s) << endl;
cout << "display integer " << (s.x) << endl;//in double it not stored the data
same as in integer thats why iteger is empty
cout << "display char " << (s.y) << endl;
cout << "display double " << (s.z) << endl;
system("pause");
}
TASK NO 3
#include <iostream>
#include<string>
using namespace std;
class Student {
public:
int id;
string name;
void insert(int i, string n)
{
id = i;
name = n;
}
void display()
{
cout << "id="<<id<<endl;
cout << "name = "<< name<<endl;
}
};
int main(void) {
Student s1;
Student s2;
s1.insert(201, "Sonoo");
s2.insert(202, "Monoo");
s1.display();
s2.display();
system("pause");
}
TASK 4
#include <iostream>
using namespace std;
// create a class
class Room {
public:
double length;
double breadth;
double height;
Room()
{
length=0;
breadth=0;
height=0;
}
double calculateArea() {
return length * breadth;
}
double calculateVolume() {
return length * breadth * height;
}
};
int main() {
Room room1;
// assign values to data members length = 45.3, breadth = 65.4, and height = 45.6
room1.length = 45.3;
room1.breadth = 65.4;
room1.height = 45.6;
// calculate and display the area and volume of the room
cout << "------------------------calculateArea()----------------------------" <<
endl;
cout << room1.calculateArea()<<endl;
cout << "------------------------calculateVolume()----------------------------" <<
endl;
cout << room1.calculateVolume()<<endl;
system("pause");
}
TASK NO 5
#include <iostream>
using namespace std;
// create a class
class Room {
public:
double length;
double breadth;
double height;
Room()
{
length = 0;
breadth = 0;
height = 0;
}
Room(double x , double y, double z)
{
length = x;
breadth = y;
height= z;
}
double calculateArea() {
return length * breadth;
}
double calculateVolume() {
return length * breadth * height;
}
};
int main() {
Room room1;
Room room2;
// assign values to data members length = 45.3, breadth = 65.4, and height = 45.6
room1.length = 45.3;
room1.breadth = 65.4;
room1.height = 45.6;
room2.length = 60.3;
room2.breadth = 67.4;
room2.height = 45.6;
// calculate and display the area and volume of the room
cout << "------------------------calculateArea()----------------------------" <<
endl;
cout << room1.calculateArea() << endl;
cout << "------------------------calculateVolume()----------------------------" <<
endl;
cout << room1.calculateVolume() << endl;
// calculate and display the area and volume of the room
cout << "------------------------calculateArea()----------------------------" <<
endl;
cout << room2.calculateArea() << endl;
cout << "------------------------calculateVolume()----------------------------" <<
endl;
cout << room2.calculateVolume() << endl;
system("pause");
}
TASK NO 6
// Example: Explicit copy constructor
#include <iostream>
using namespace std;
class Sample
{
int id;
int age;
int salary;
int employ_no;
public:
void init(int x,int y,int z,int a)
{
id = x;
age=y;
salary=z;
employ_no=a;
}
Sample() {} //default constructor with empty body
TASK NO 7
#include <iostream>
#include <string>
using namespace std;
class Employee {
private:
// Private attribute
int salary;
int age;
char name;
string desigination;
int employ_code;
public:
// Setter
void setSalary(int s, string ch,int n) {
salary = s;
desigination=ch;
employ_code=n;
}
// Getter
int getSalary() {
return salary;
}
string desigina() {
return desigination;
}
int employ()
{
return employ_code;
}
};
int main() {
Employee myObj;
//myObj.salary will be error
myObj.setSalary(50000,"teacher",108);
cout << "-----------display salary-------------------"<<endl;
cout << myObj.getSalary() << endl;
cout << "-----------display desigination of employ-------------------"<<endl;
cout<<myObj. desigina()<<endl;
cout << "-----------display employ code-------------------"<<endl;
cout << myObj.employ()<<endl;
system ("pause");
}
TASK NO 8
#include <iostream>
#include <string>
using namespace std;
class Date {
private:
int month;
int day;
int year;
public:
Date(int m, int d, int y) : month(m), day(d), year(y) {}
int main() {
Date d(12, 25, 2010);
// Print the date in all three formats
cout << "--------------------------date in mm / dd / yy
format----------------------------------" << endl;
d.print1();
cout << "--------------------------date in Month day, year
format----------------------------------" << endl;
d.print2();
cout << "--------------------------date in day Month year
format----------------------------------" << endl;
d.print3();
system("pause");
}