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

Aleena Akram 22F-8801 Lab 6: Task 1

The document contains code for 8 programming tasks. Task 1 defines an enum for days of the week and uses it to print the day before and after Monday. Task 2 defines a union with integer, char, and double members and prints their values. Task 3 defines a Student class with name and id and prints object details. Task 4 defines a Room class with area and volume calculation methods. Task 5 adds a constructor to the Room class. Task 6 demonstrates an explicit copy constructor. Task 7 uses getter and setter methods to access private class members. Task 8 defines a Date class with methods to print dates in different formats.

Uploaded by

f228802
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Aleena Akram 22F-8801 Lab 6: Task 1

The document contains code for 8 programming tasks. Task 1 defines an enum for days of the week and uses it to print the day before and after Monday. Task 2 defines a union with integer, char, and double members and prints their values. Task 3 defines a Student class with name and id and prints object details. Task 4 defines a Room class with area and volume calculation methods. Task 5 adds a constructor to the Room class. Task 6 demonstrates an explicit copy constructor. Task 7 uses getter and setter methods to access private class members. Task 8 defines a Date class with methods to print dates in different formats.

Uploaded by

f228802
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

ALEENA AKRAM

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

Sample(Sample &t) //copy constructor


{
id = t.id;
age = t.age;
salary = t.salary;
employ_no = t.employ_no;
}
void display()
{
cout << endl << "ID=" << id;
cout << endl << "age=" << age;
cout << endl << "salary=" << salary;
cout << endl << "employ_no=" << employ_no;
}
};
int main()
{
Sample obj1;
obj1.init(10,20,30,40);
cout << "------------- before copy constructor-----------------------" << endl;
obj1.display();
cout << "------------- copy constructor-----------------------" << endl;
Sample obj2(obj1); //or obj2=obj1; copy constructor called
obj2.display();
system("pause");
}

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) {}

// Member function to print date in "mm/dd/yy" format


void print1() const {
cout << month << "/" << day << "/" << year % 100 << endl;
}

// Member function to print date in "Month day, year" format


void print2() const {
string months[] = { "January", "February", "March", "April","May", "June",
"July", "August","September", "October", "November", "December" };
cout << months[month - 1] << " " << day << ", " << year << endl;
}

// Member function to print date in "day Month year" format


void print3() const {
string months[] = { "January", "February", "March", "April","May", "June",
"July", "August","September", "October", "November", "December" };
cout << day << " " << months[month - 1] << " " << year << endl;
}
};

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

You might also like