You are on page 1of 6

Q1

#include<iostream>

#include<string>

using namespace std;

struct BDate

int m, d, y;

};

class SDate

private:

string name, address;

BDate BD;

public:

void getData()

cout << "Enter your name : ";

getline(cin, name);

cout << "Enter your address : ";

getline(cin, address);

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

cin >> BD.d >> BD.m >> BD.y;

char Check()

if (BD.d > 0 && BD.d < 32 && BD.m < 13 && BD.m>0 && BD.y>1899 && BD.y < 2020)

return 'y';

else

return 'n';
}

void ifNotValidDate()

while (Check() != 'y')

cout << "Enter a valid date" << endl;

cin >> BD.d >> BD.m >> BD.y;

void print()

cout << "Name : " << name << endl;

cout << "Address : " << address << endl;

cout << "BirthDate : " << BD.d << '/' << BD.m << '/' << BD.y << endl;

};

int main()

SDate S;

S.getData();

S.Check();

S.ifNotValidDate();

S.print();

system("pause");

return 0;

}
Q2

#include<iostream>

using namespace std;

class Time

private:

int hours, minutes, seconds;

public:

Time()

hours = 0;

minutes = 0;

seconds = 0;

Time(int h, int m, int s)

hours = h;

minutes = m;

seconds = s;

Time(Time& T1)

hours = T1.hours;

minutes = T1.minutes;

seconds = T1.seconds;

void showTime() const

cout << hours << ':' << minutes << ':' << seconds << endl;
}

void showTime2()

cout << hours++ << ':' << minutes++ << ':' << seconds-- << endl;

};

int main()

Time T1,T2(T1);

const Time T3(2,4,3),T4;

T1.showTime();

T2.showTime();

T3.showTime();

T4.showTime();

T1.showTime2();

T2.showTime2();

//T3.showTime2(); not valid because it's const obj and the fn does change the value of var

//T4.showTime2();

system("pause");

return 0;

}
Q3

#include<iostream>

#include<string>

using namespace std;

class item

string name;

int code, price;

char ws;

static int serial;

public:

item()

code = serial;

serial += 1000;

void setData()

cout << "Enter name : ";

getline(cin,name);

cout << "Enter price : ";

cin >> price;

ws = cin.get();

void showSerial()

cout << "Serial= "<<serial<<endl;

}
void showData()

cout << "Name : " << name << endl;

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

cout << "Code : " << code << endl;

};

int item::serial = 1000;

int main()

item I[5];

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

I[i].setData();

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

I[i].showSerial();

I[i].showData();

system("pause");

return 0;

You might also like