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

Assgnment Lab 8

The document defines nested classes to store phone numbers - LocalPhone stores local numbers, NatPhone extends it to add city codes, and IntPhone adds country codes. An IntPhone object is created and its display() method outputs the phone number details by calling

Uploaded by

Noor ul Ain
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)
14 views

Assgnment Lab 8

The document defines nested classes to store phone numbers - LocalPhone stores local numbers, NatPhone extends it to add city codes, and IntPhone adds country codes. An IntPhone object is created and its display() method outputs the phone number details by calling

Uploaded by

Noor ul Ain
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/ 7

1)input:

#include<iostream>
using namespace std;
class calculator
{
public:
int sum = 0;
int add(int a, int b)
{
sum = a + b;
return sum;
}
float add(float a, float b, float c)
{
sum = a + b + c;
return sum;
}
double add(double a, double b)
{
sum = a + b;
return sum;
}
};
int main()
{
calculator c1;
int result1 = c1.add(10,5);

cout << "sum of two integers are " << result1 << endl;
float result2 = c1.add(11.1,33.9,2.6);
cout << "sum of three floats are " << result2 << endl;
double result3 = c1.add(2.56,7.345);
cout << "sum of two doubles are " << result3 << endl;
return 0;

Output:

2)input:
#include<iostream>
using namespace std;
class simple
{
protected:
int a;
int b;
int sum = 0;
int diff = 0;
float pro = 0.0;
float divide = 0.0;
simple(int n1, int n2)
{
a = n1;
b = n2;
}
void add()
{
sum = a + b;
cout << "addition of two integers are " << sum << endl;
}
void sub()
{
diff = a - b;
cout << "subtraction of 2 integers are " << diff << endl;
}
void mul()
{
pro = a * b;
cout << "the multipy of two integers are " << pro << endl;
}
void div()
{
if (b != 0)

{
divide = a / b;
cout << "the division of two integers are " << divide << endl;
}
else
{
cout << "ERROR:cannot divide by zero" << endl;
}
}

};
class Complex :public simple
{
public:
Complex(int n1, int n2) :simple( n1, n2)
{
a = n1;
b = n2;
}
void add()
{
if (a > 0 && b > 0)
{
simple::add();
}
else
{
cout << "ERROR: invalid input" << endl;
}
}
void sub()
{
if (a > 0 && b > 0)
{
simple::sub();
}
else
{
cout << "ERROR: invalid input" << endl;
}
}
void mul()
{
if (a > 0 && b > 0)
{
simple::mul();
}
else
{
cout << "ERROR: invalid input" << endl;
}
}
void div()
{
if (a > 0 && b > 0)
{
simple::div();
}
else
{
cout << "ERROR: invalid input" << endl;
}
}
};
int main()
{
Complex c1(5, 10);
c1.add();
c1.sub();
c1.mul();
c1.div();
return 0;
}

Output:
3)input:
#include<iostream>
using namespace std;
class localphone
{
public:
double phone;
void input()
{
cout << "enter the local telephone number" << endl;
cin >> phone;
}
void output()
{
cout << "the local phone number is " << phone << endl;
}
};
class NatPhone :public localphone
{
public:
int citycode;
void input()
{
cout << "enter the city code." << endl;
cin >> citycode;
}
void output()
{
cout << "the city code number is " << citycode << endl;
}
};
class IntPhone :public NatPhone
{
public:
int countrycode;
void input()
{
cout << "enter the country code." << endl;
cin >> countrycode;
}
void output()
{
cout << "the country code is " << countrycode << endl;
}
void display()
{
cout << "details for phone number." << endl;
cout << endl;
localphone::input();
localphone::output();
cout << endl;
cout << "details for city code." << endl;
cout << endl;
NatPhone::input();
NatPhone::output();
cout << endl;
cout << "details for country code." << endl;
cout << endl;
IntPhone::input();
IntPhone::output();

}
};

int main()
{
IntPhone in;
in.display();
return 0;

Output:

4)input:
#include<iostream>
#include<string>
using namespace std;
class Book
{
public:
int id;
string name;
int price;
void input()
{
cout << "enter the id of book." << endl;
cin >> id;
cout << "enter the name of book." << endl;
cin >> name;
cout << "enter the price of book." << endl;
cin >> price;
}
void show()
{
cout << "the id of book is" <<id<< endl;
cout << "the name of book is " <<name<< endl;
cout << "the price of book is " <<price<< endl;
}

};
class Writer
{
public:
string writer_name;
string address;
int no_of_books;
Book b[5];
void inputWriter()
{
cout << "Enter Writer Name: " << endl;
getline(cin, writer_name);
cout << "Enter Address: " << endl;
getline(cin, address);
cout << "Enter Number of Books Written: " << endl;
cin >> no_of_books;
cin.ignore();
for (int i = 0; i < no_of_books; i++)
{
std::cout << "Enter details for Book " << i + 1 << ":" << std::endl;
b[i].input();
}
}
void displayWriter()
{
cout << "Writer Name is: " << writer_name << endl;
std::cout << "Address of writer is: " << address << endl;
std::cout << "Number of Books Written: " << no_of_books << endl;

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


std::cout << "\nDetails for Book " << i + 1 << ":" << endl;
b[i].show();
}
}
};
int main()
{
Writer w1;

cout << "Enter details of Writer:" << endl;


w1.inputWriter();

cout << "Writer Details:" << endl;


w1.displayWriter();

return 0;
}

Output:
5)input:

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy