Ex18 :
A phone company asked for an application that has a class called Costumer with three data members (name, phoneNumber, and credit) and two methods one to display the client’s information and the other to top-up (recharge) the credit. The method that allows the clients to make a call (makeCall) is defined in a class called Calling. The implementations of (makeCall) function are deduct a certain amount form the credit according to the call duration (200 IQD per minute) and issue a message “Insufficient credit to make the call.” in case the credit is less than the cost per minute. Define a class called Client that can inherit data members and methods from
both classes. Use an appropriate setter function to initialize the variables.
A phone company asked for an application that has a class called Costumer with three data members (name, phoneNumber, and credit) and two methods one to display the client’s information and the other to top-up (recharge) the credit. The method that allows the clients to make a call (makeCall) is defined in a class called Calling. The implementations of (makeCall) function are deduct a certain amount form the credit according to the call duration (200 IQD per minute) and issue a message “Insufficient credit to make the call.” in case the credit is less than the cost per minute. Define a class called Client that can inherit data members and methods from
both classes. Use an appropriate setter function to initialize the variables.
#include <iostream>
using namespace std;
class Costumer
{
protected:
string name;
int PhoneNumber;
int credit;
public:
void set(string name,int PhoneNumber ,int credit){
this->name=name;
this-> PhoneNumber=PhoneNumber;
this->credit=credit;
}
void getCredit(int &a){
a=credit;
}
void top_up(int a){
credit+=a;
}
void display(){
cout<<"name:"<<name<<endl<<"PhoneNumber:"<<PhoneNumber<<endl<<"credit:"<<credit;
}
};
class Calling{
public:
int makeCall(Costumer& c,int s){
int a;
c.getCredit(a);
if(a<s*200){
cout<<"Insufficient credit to make the call"<<endl;
}else {
c.top_up(-s*200);
return a;
}
}
};
class client:public Costumer,public Calling{
public:
void set(string name,int PhoneNumber ,int credit){
this->name=name;
this-> PhoneNumber=PhoneNumber;
this->credit=credit;
}
};
int main() {
client c1;
c1.set("yas",3459678,1000);
c1.makeCall(c1,3);
c1.top_up(90);
c1.display();
return 0;
}
❤5👍1
شيئية OOP
سؤال الصباحي
الجواب مقدم من أحد طلبة الصباحي وفقهم الله :
#include <iostream>
using namespace std;
class Cuboid {
private:
double length;
double breadth;
double height;
public:
Cuboid(double l, double b, double h) {
length = l;
breadth = b;
height = h;
}
double Volume() {
return length * breadth * height;
}
Cuboid operator+(Cuboid& cuboid2){
Cuboid cuboid3(0, 0, 0);
cuboid3.length = length + cuboid2.length;
cuboid3.breadth = breadth + cuboid2.breadth;
cuboid3.height = height + cuboid2.height;
return cuboid3;
}
};
int main() {
Cuboid cuboid1(4, 3, 2);
Cuboid cuboid2(3, 1, 2);
Cuboid cuboid3(0, 0, 0);
cout << "cuboid1 : " << cuboid1.Volume() << endl;
cout << "cuboid2 : " << cuboid2.Volume() << endl;
cuboid3 = cuboid1 + cuboid2;
cout << "combinedCuboid : " << cuboid3.Volume() << endl;
return 0;
}
👍4❤3
مثال عن polymorphism :
#include <iostream>
using namespace std;
class Shape {
public:
virtual double area() = 0;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) {
radius = r;
}
double area() {
return 3.14 * radius * radius;
}
};
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle(double l, double w): length(l), width(w) {}
double area() {
return length * width;
}
};
int main() {
Circle circle(5);
Rectangle rectangle(4, 6);
cout << "Area in Circle : " << circle.area() << endl;
cout << "Area in Rectangle : " << rectangle.area() << endl;
return 0;
}
👍5❤1
شيئية OOP
مثال عن polymorphism : #include <iostream> using namespace std; class Shape { public: virtual double area() = 0; }; class Circle : public Shape { private: double radius; public: Circle(double r) { radius = r; } double area() { return…
نفس فكرة وطريقة الملزمة :
#include <iostream>
using namespace std;
class Shape {
public:
virtual double area() = 0;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r): radius(r){}
double area() {
return 3.14 * radius * radius;
}
};
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle(double l, double w): length(l), width(w) {}
double area() {
return length * width;
}
};
int main() {
Shape *Shape;
Circle circle(5);
Rectangle rectangle(4, 6);
Shape = &circle;
cout << "Area in Circle : " << Shape->area() << endl;
Shape = &rectangle;
cout << "Area in Rectangle : " << Shape->area() << endl;
return 0;
}
👍3❤1
شيئية OOP
سؤال مختبر البرمجة بتاريخ: 2024/4/20
#include <iostream>
using namespace std;
class cakestore{
public:
virtual void print()
{
cout<< "this is cake store"<<endl;
}
};
class chocolate: public cakestore {
public:
void print() {
cout<< "chocolate cake" << endl;
}
};
class vanilla: public cakestore {
public:
void print() {
cout<< "vanilla cake" << endl;
}
};
int main() {
chocolate typechocolate;
typechocolate.print();
vanilla typevanilla;
typevanilla.print();
/*
cakestore *caketype;
chocolate typecake;
caketype = &typecake;
caketype->print();
vanilla typevanilla;
caketype = &typevanilla;
caketype->print();
*/
return 0;
}
❤5👍1
شيئية OOP
منا وانت نازل مادتنا لهذا الكورس : أمثلة الكوز اسئلة المختبر ال18 سؤال اسئلة من العام السابق و بالنسبة لشروحات المادة ف اليوتيوب مليئ بالمقاطع تابع الي تفهم عليه +انا ناشر لعادل نسيم : في نهاية القائمة ( بوينتر، رفرنس، ستركت، Inline ) : https://youtub…
#include <iostream>
using namespace std;
class Box {
private:
double length;
double breadth;
double height;
public:
Box(double l, double b, double h) {
length = l;
breadth = b;
height = h;
}
double Volume() {
return length * breadth * height;
}
int compare(Box box) {
return this->Volume() > box.Volume();
}
};
int main(void)
{
Box Box1(3.3, 1.2, 1.5);
Box Box2(8.5, 6.0, 2.0);
if(Box1.compare(Box2)) {
cout << "Box2 is smaller than Box1" <<endl;
}
else {
cout << "Box2 is equal to or larger than Box1" <<endl;
}
return 0;
}
هاي من الملزمة بس اتوقع يجيبها لأن ما طبقناها
👍6
شيئية OOP
Photo
السؤال الأول /
1- instance
2- memory
3- name
4- member
5- this ->
6- early
7- virtual , implementation
☆●☆
السؤال الثاني /
☆●☆
السؤال الثالث /
1- instance
2- memory
3- name
4- member
5- this ->
6- early
7- virtual , implementation
☆●☆
السؤال الثاني /
#include<iostream>
using namespace std;
class Cars {
private:
string brandN;
int maxspeed, fuelC, safetyR;
public:
Cars(string b,int m,int f,int s):brandN(b),maxspeed(m),fuelC(f),safetyR(s){}
bool operator>(const Cars& other) const {
int pointA=0, pointB=0;
if (maxspeed>other.maxspeed) {
pointA++;
} else {
pointB++;
}
if (fuelC<other.fuelC) {
pointA++;
} else {
pointB++;
}
if (safetyR>other.safetyR) {
pointA++;
} else {
pointB++;
}
return pointA > pointB;
}
};
int main() {
Cars BMW("BMW",300,5,10);
Cars KIA("KIA",200,4,5);
if(BMW > KIA) {
cout <<"BMW has a higher assessment"<< endl;
}else{
cout <<"KIA has a higher assessment"<< endl;
}
return 0;
}
☆●☆
السؤال الثالث /
#include<iostream>
using namespace std;
class customer
{
protected:
string name;
int phonenumber;
int credit;
public:
customer(string n,int ph,int c):name(n),phonenumber(ph),credit(c){}
virtual void call() {
cout << " virtual calling " << endl;
};
};
class prepaid : public customer
{
public:
prepaid(string n,int ph,int c):customer(n,ph,c){}
void call() {
cout<<"prepaid make call"<<endl;
}
};
class postpaid : public customer
{
public:
postpaid(string n,int ph,int c):customer(n,ph,c){}
void call()
{
cout<<"postpaid make call"<<endl;
}
};
int main()
{
prepaid call1("memo",3579,300);
postpaid call2("oeom",2468,600);
customer* ptr=&call1;
ptr->call();
ptr=&call2;
ptr->call();
}
👍3❤2
هندسة حاسبات
Photo
حل السؤال
#include <iostream>
using namespace std;
class Bank {
public:
double balance;
void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
else {
throw "can't deposit a negtive value";
}
}
void withDrawing(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
throw "can't withDrawing more money than your balance";
}
}
void display() {
cout << "your Balance: " << balance << endl;
}
};
int main() {
Bank s1 = {350};
try {
s1.deposit(-5);
} catch (const char* msg) {
cerr << "Error: " << msg << endl;
}
try {
s1.withDrawing(400);
} catch (const char* msg) {
cerr << "Error: " << msg << endl;
}
s1.display();
return 0;
}
👍6❤3
شيئية OOP
كوز اليوم بتاريخ : 2024/4/29
الحل :
#include<iostream>
using namespace std;
class carengine {
public:
virtual void check()=0;
};
class Water : public carengine {
private:
int watertemperature;
public:
Water(int w = 0):watertemperature(w){}
void check()
{
if(watertemperature<30) {
throw "water temperature is too low";
}
else {
cout << "water temperature normal" << endl;
}
}
};
class Oil : public carengine {
private:
int oiltemperature;
public:
Oil(int o = 0):oiltemperature(o){}
void check()
{
if(oiltemperature>75) {
throw "oil temperature is too high";
}
else {
cout << "oil temperature normal" << endl;
}
}
};
int main()
{
Water check1(20);
Oil check2(90);
carengine* eng;
eng = &check1;
try {
eng->check();
} catch(const char* msg) {
cerr << "Note: " << msg << endl;
}
eng = &check2;
try {
eng->check();
} catch(const char* msg) {
cerr << "Note: " << msg << endl;
}
return 0;
}
❤6❤🔥2👍1🌚1
اوفرلودنك =<
#include <iostream>
using namespace std;
class Box {
private:
double length;
double breadth;
double height;
public:
Box(double l, double b, double h): length(l), breadth(b), height(h) {}
double Volume() const {
return length * breadth * height;
}
bool operator>=(const Box& box) {
return Volume() >= box.Volume();
}
};
int main()
{
Box Box1(3, 9, 5);
Box Box2(8, 6, 2);
if (Box1 >= Box2) {
cout << "Box1 is equal to or bigger than Box2" << endl;
}
else {
cout << "Box1 is smaller than Box2" << endl;
}
return 0;
}
👍6❤3🔥3
شيئية OOP
اسئلة فاينل المختبر مجموعة 1 : 1- بوليمورفزم ( بالملزمة موجود ( شكل ، الريكتانكل ، تراينكل ) ص45-44 2- كاتش وثرو ( القسمة اخر صفحة بالملزمة ) ولكن كلاس مجموعة 2 : 1- سؤال البنك ( فريند فنكشن ، سكوب ريزلوشن ( : : ) ) 2- اوفرلودنك =< مجموعة 3 : 1- اوفرلودنك…
سؤال القسمة try , catch , throw :
#include <iostream>
using namespace std;
class calc{
private:
int x, y;
public:
calc(int m, int g): x(m), y(g) {}
double addition () {
return x+y;
}
double subtract () {
return x-y;
}
double multiply () {
return x*y;
}
double divide () {
if ( y == 0 ) {
throw "Division by zero condition!";
}
else {
return x/y;
}
}
};
int main() {
calc check(7, 0);
try {
cout << check.divide();
} catch ( const char* msg ) {
cerr << msg << endl;
}
return 0;
}
👍4❤2
هنا صيغة طلب السؤال تكون التعامل مع المشاكل يكون في الكلاس نفسه
#include <iostream>
using namespace std;
class Bank {
public:
double balance;
void deposit(double amount) {
try {
if (amount > 0) {
balance += amount;
} else {
throw "can't deposit a negative value";
}
} catch (const char* msg) {
cerr << "deposit Error: " << msg << endl;
}
}
void withDrawing(double amount) {
try {
if (balance >= amount) {
balance -= amount;
} else {
throw "can't withdraw more money than your balance";
}
} catch (const char* msg) {
cerr << "withdrawal Error: " << msg << endl;
}
}
void display() {
cout << "your balance: " << balance << endl;
}
};
int main() {
Bank s1 = {350};
s1.deposit(-5);
s1.withDrawing(400);
s1.display();
return 0;
}
👍1
شيئية OOP
مثال عن polymorphism : #include <iostream> using namespace std; class Shape { public: virtual double area() = 0; }; class Circle : public Shape { private: double radius; public: Circle(double r) { radius = r; } double area() { return…
هذا السؤال
بس يكلك سويه بالابستراكشن
هيج يصير
اول يكلك سويه بمفهوم توقع المشاكل ( catch , try , throw )
ايضا سهل
جربوه بطريقكم
بس يكلك سويه بالابستراكشن
هيج يصير
#include <iostream>
using namespace std;
class Shape {
public:
virtual void area() = 0;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r): radius(r){}
void area() {
cout << "Circle area: "<< 3.14 * radius * radius << endl;
}
};
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle(double l, double w): length(l), width(w) {}
void area() {
cout << "Rectangle area: " << length * width << endl;
}
};
int main() {
Circle circle(5);
Rectangle rectangle(4, 6);
circle.area();
rectangle.area();
return 0;
}
اول يكلك سويه بمفهوم توقع المشاكل ( catch , try , throw )
ايضا سهل
جربوه بطريقكم
❤4🔥1
شيئية OOP
Photo
Q2/
A/
B/
A/
#include<iostream>
using namespace std;
class Rectangle
{
protected:
double lenght,width;
public:
Rectangle(double l,double w):lenght(l),width(w){}
};
class cuboid : public Rectangle
{
double height;
public:
cuboid(double l,double w,double h):Rectangle(l,w),height(h){}
void volume()
{
cout<<"area of cuboid : "<<lenght*width*height<<endl;
}
};
int main()
{
cuboid r(2.2,10.5,5.2);
r.volume();
}
B/
#include<iostream>
using namespace std;
class Cars {
private:
string brandN;
int maxspeed, fuelC, safetyR;
public:
Cars(string b,int m,int f,int s):brandN(b),maxspeed(m),fuelC(f),safetyR(s){}
bool operator>(const Cars& other) const {
int pointA=0, pointB=0;
if (maxspeed>other.maxspeed) {
pointA++;
} else {
pointB++;
}
if (fuelC<other.fuelC) {
pointA++;
} else {
pointB++;
}
if (safetyR>other.safetyR) {
pointA++;
} else {
pointB++;
}
return pointA > pointB;
}
};
int main() {
Cars BMW("BMW",300,5,10);
Cars KIA("KIA",200,4,5);
if(BMW > KIA) {
cout <<"BMW has a higher assessment"<< endl;
}else{
cout <<"KIA has a higher assessment"<< endl;
}
return 0;
}
❤4❤🔥4💊3👍1
شيئية OOP
Photo
Q3/
A/
B/
A/
#include<iostream>
using namespace std;
class Item
{
protected:
string title,author;
int ISBN;
public:
Item(string t,string a,int i):title(t),author(a),ISBN(i){}
virtual void buy(bool p){
cout << "the virtual function" << endl;
}
};
class Book : public Item
{
public:
Book(string t,string a,int i):Item(t,a,i){}
void buy(bool p)
{
if(p)
{
cout<<title<<" by "<<author<<endl<<"ISBN :"<<ISBN<<endl;
}
else
{
cout<<"book unavailable"<<endl;
}
}
};
class DVD : public Item
{
public:
DVD(string t,string a,int i):Item(t,a,i){}
void buy(bool p)
{
cout<<title<<" by "<<author<<endl<<"ISBN :"<<ISBN<<endl;
}
};
int main() {
Book adeq("legend of zelda","ahmed",503);
DVD n("mario songs","marimaker",163);
Item* ptr;
ptr=&adeq;
ptr->buy(true);
ptr=&n;
ptr->buy(0);
}
B/
#include<iostream>
#include<stdexcept>
using namespace std;
class nuclear_reactor
{
protected:
int temperature,pressure;
double radiation_levels;
public:
nuclear_reactor(int t,int p,double r):temperature(t),pressure(p),radiation_levels(r){}
};
class monitoring : public nuclear_reactor
{
public:
monitoring(int t,int p,double r):nuclear_reactor(t,p,r){}
void checktemp()
{
if(temperature>1000)
{
throw "temperature is too high";
}
else cout<<"temperature is normal"<<endl;
}
void checkpressure(){
if (pressure>2000)
{
throw"pressure is too high";
}
else cout<<"pressure is normal"<<endl;
}
void checkrad() {
if (radiation_levels>0.1)
{
throw"high radiation";
}
else cout<<"normal radiation"<<endl;
}
};
int main() {
monitoring a(1100,1700,0.03);
monitoring b(900,1500,0.02);
try{
a.checktemp();
a.checkpressure();
a.checkrad();
}catch(const char* msg)
{
cerr<<msg <<endl;
}
try{
b.checktemp();
b.checkpressure();
b.checkrad();
}catch(const char* msg)
{
cerr<<msg <<endl;
}
}
❤6
شيئية OOP
Photo
Q1 /
A-
1) 21
2) 1
3) 1
4) 8
5) 48
B-
1- relational
2- logical
3- ( &= )
4- continue
5- beginning
ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
Q2/
A - يشبه سؤال 2
B - سؤال 4
ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
Q3/
A - 6 سؤال
B - سؤال 16
A-
1) 21
2) 1
3) 1
4) 8
5) 48
B-
1- relational
2- logical
3- ( &= )
4- continue
5- beginning
ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
Q2/
A - يشبه سؤال 2
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10;
cout << "Before swapping: a = " << a << ", b = " << b << endl;
a = a + b;
b = a - b;
a = a - b;
cout << "After swapping: a = " << a << ", b = " << b << endl;
return 0;
}
B - سؤال 4
#include <iostream>
using namespace std;
int main()
{
float sum=0;
float average;
float number [6];
cout<< " enter the six number : "<<endl;
for (int i = 0; i < 6; i++)
{
cout<<" number "<<i+1<<" : ";
cin>>number[i];
sum += number[i];
average = sum/6;
}
cout<<" the sum "<<sum<<endl;
cout<<" the average "<<average<<endl;
return 0;
}
ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
Q3/
A - 6 سؤال
#include <iostream>
using namespace std;
int main ()
{
int a = 11;
while ( a <= 29)
{
cout<<a<<", ";
a += 2;
}
return 0;
}
B - سؤال 16
#include <iostream>
using namespace std;
int main()
{
char character;
cout << "Enter a character: ";
cin >> character;
if ((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z'))
{
switch (character)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
cout << character << " is a vowel." << endl;
break;
default:
cout << character << " is a consonant." << endl;
}
}
else
{
cout << "Invalid input. Please enter a valid alphabet character." << endl;
}
return 0;
}
❤2
شيئية OOP
Photo
Q4/
A -
👇همين يعتبر حل حبيته هيج :
B - سؤال 33
ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
Q5/
A -
B -
ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
Q6/
A -
B -
A -
#include <iostream>
using namespace std;
int fun(int a)
{
return a;
}
int main() {
cout<<fun(5);
}
👇همين يعتبر حل حبيته هيج :
#include <iostream>
using namespace std;
string fun(string a ) {
return a;
}
int main() {
cout<<fun( " نجحنا واليحبنا يفرح ويانا ");
}
B - سؤال 33
#include <iostream>
using namespace std;
int main ()
{
char grade ;
cout << " take one from this ( A , B, C, D, F) ";
cin >> grade;
switch(grade)
{
case 'A' :
cout << "Excellent!" << endl;
break;
case 'B' :
cout << "Well done" << endl;
break;
case 'C' :
cout << "done" << endl;
break;
case 'D' :
cout << "You passed" << endl;
break;
case 'F' :
cout << "Better try again" << endl;
break;
default :
cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl;
return 0;
}
ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
Q5/
A -
#include<iostream>
using namespace std;
int Sum(int a[],int s)
{
int sum = 0;
for (int i = 0; i < s; i++) {
int current_item = a[i];
bool unique_ = true;
for (int j = 0; j < s; j++) {
if (i != j&¤t_item == a[j]) {
unique_ = false;
}
}
if (unique_)sum += a[i];
}
return sum;
}
int main() {
int i[8] = {1,2,3,2,3,4,2,5};
cout << Sum(i,8);
}
B -
#include<iostream>
using namespace std;
double product(int a,double d){
return a*d;
}
int main() {
cout<<(2,1.5);
}
ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
Q6/
A -
#include<iostream>
using namespace std;
int power(int base, int pow) {
if (pow == 0)
return 1;
else if (pow == 1)
return base;
else
return base * power(base, pow - 1);
}
int main() {
cout << power(3, 3);
return 0;
}
B -
#include<iostream>
using namespace std;
double calculateSin(double x, int n)
{
double result = 0;
double term = 1;
int sign = 1;
for (int i = 1; i <= n; ++i)
{
result += sign * term;
sign = -sign;
term *= (x * x) / ((2 * i) * (2 * i + 1));
}
return result;
}
int main()
{
double x;
int n;
cout << "Enter the value of x in radians: ";
cin >> x;
cout << "Enter the number of terms in the Taylor series approximation: ";
cin >> n;
double sinApproximation = calculateSin(x, n);
cout << "Approximation of sin(" << x << ") using Taylor series with " << n << " terms: " << sinApproximation << endl;
return 0;
}
❤1
شيئية OOP
Photo
Q1/
1-instance
2-blueprint
3-constructor
4-inheritance
5-encapsulation
Q2/
Q3/
Q4/
Q5/
1-instance
2-blueprint
3-constructor
4-inheritance
5-encapsulation
Q2/
#include <iostream>
using namespace std;
class Circle{
protected:
float radius;
public:
Circle(float r):radius(r){}
};
class Cylinder : public Circle{
private:
float height;
public:
Cylinder(float r,float h):Circle(r),height(h){}
float volume(){
return (3.14*radius*radius*height);
}
};
int main(){
Cylinder c(3.5,6.1);
cout<<c.volume();
}
Q3/
#include <iostream>
using namespace std;
class smart_phone{
int battery_life,camera_quality,user_interface;
public:
smart_phone(int b,int c,int u):battery_life(b),camera_quality(c),
user_interface(u){}
bool operator > (smart_phone iphone){
int s_score=0,i_score=0;
if(battery_life>iphone.battery_life)s_score++;
else i_score++;
if(camera_quality>iphone.camera_quality)s_score++;
else i_score++;
if(user_interface>iphone.user_interface)s_score++;
else i_score++;
return s_score>i_score;
}
};
int main(){
smart_phone samsung(9,9,9);
smart_phone iphone(1,1,1);
if(samsung>iphone)cout<<"first phone is superior";
else cout<<"second phone is superior";
}
Q4/
#include <iostream>
using namespace std;
class bank {
private :
string AccountHolderName;
int AccountNumber;
double balance;
public:
bank ( string AH, int AN, double B) {
AccountHolderName = AH;
AccountNumber = AN;
balance = B;
}
void deposit (double amount) {
balance += amount;
}
void withDrawal(double amount);
friend void display (bank bank);
};
void bank::withDrawal(double amount) {
balance -= amount;
}
void display (bank bank) {
cout << "your balance is : " << bank.balance << endl;
};
int main()
{
bank client = {"mogo", 78904, 1090};
client.deposit(24.5);
client.withDrawal(9.5);
display(client);
return 0;
}
Q5/
#include <iostream>
using namespace std;
class Vehicle {
protected:
string Make;
int Model,LicensePlate;
public:
Vehicle(string m,int mo,int l)
: Make(m), Model(mo), LicensePlate(l) {}
virtual void rent(){
cout << "the virtual function" << endl;
};
};
class Car : public Vehicle {
bool serviced;
public:
Car(string m,int mo,int l, bool s)
: Vehicle(m,mo,l), serviced(s) {}
void rent(){
if (serviced) {
cout<<"Car rented successfully"<<endl;
} else {
cout<<"Car cannot be rented"<<endl;
}
}
};
class Truck : public Vehicle {
public:
Truck(string m, int mo, int l)
: Vehicle(m, mo, l) {}
void rent(){
cout<<"Truck rented"<<endl;
}
};
int main() {
Car c("crown",2000, 61947, true);
Truck t("Ford",2010 , 13512);
Vehicle* ptr;
ptr = &c;
ptr->rent();
ptr = &t;
ptr->rent();
}
❤1
شيئية OOP
Q1/ 1-instance 2-blueprint 3-constructor 4-inheritance 5-encapsulation Q2/ #include <iostream> using namespace std; class Circle{ protected: float radius; public: Circle(float r):radius(r){} }; class Cylinder : public Circle{ private: float…
Q6/
#include<iostream>
using namespace std;
class SecuritySystem{
protected:
bool MotionDetection;
string DoorWindow_Status;
int FireSmoke_Levels;
public:
SecuritySystem(bool MD, string DWS, int FSL): MotionDetection(MD), DoorWindow_Status(DWS), FireSmoke_Levels(FSL) {}
};
class HomeSecurity : public SecuritySystem {
public:
HomeSecurity(bool MD, string DWS, int FSL): SecuritySystem(MD, DWS, FSL) {}
void checkMD(){
if(MotionDetection){
throw "there is a Motion";
}
else
cout << "there is no Motion" << endl;
}
void checkDWS(){
if(DoorWindow_Status == "open") {
throw "the Door and Window are not closed";
}
else
cout << "the Door and Window are close" << endl;
}
void checkFSL(){
if(FireSmoke_Levels > 50) {
throw "the fire and somkes levels are not normal";
}
else
cout << "the fire and somkes levels are normal" << endl;
}
};
int main() {
HomeSecurity home1( false, "closed", 23);
try {
home1.checkMD();
home1.checkDWS();
home1.checkFSL();
} catch(const char* msg){
cerr << msg <<endl;
}
}
❤1👏1
شيئية OOP
Photo
Q1/A
1-(X+=Y)=57
2-(X||Y)=1(True)
3-(X!=Y)=1(True)
4-(X^=4)=34
5-(X»=4)=2
Q1/B
1-Instructions
2-logical
3-sizeof
4-continue
5-start
Q2/A
Q2/B
Q3/A
Q3/B
Q4/
Q5/
1-(X+=Y)=57
2-(X||Y)=1(True)
3-(X!=Y)=1(True)
4-(X^=4)=34
5-(X»=4)=2
Q1/B
1-Instructions
2-logical
3-sizeof
4-continue
5-start
Q2/A
#include<iostream>
using namespace std;
int main () {
for(int i=50;i<=65;++i) {
if (i==57||i==62)continue;
cout<<i<<',';
}
return 0;
}
Q2/B
#include<iostream>
using namespace std;
int main() {
int salary,performance_rating,years_of_service;
float bonus=0;
cin>>salary>>performance_rating>>years_of_service;
if(years_of_service>=5) {
if (performance_rating>8) {
bonus=20;
}else {
bonus=10;
}
}else {
if (performance_rating>8) {
bonus=10;
}else {
bonus=5;
}
}
cout<<"bonus:"<<bonus<<'%'<<endl;
return 0;
}
Q3/A
#include<iostream>
using namespace std;
int main () {
char drink_size;
cin>>drink_size;
switch (drink_size) {
case 's':
cout<<"small";
break;
case'm':
cout<<"medium";
break;
case'l':
cout<<"large";
case'x':
cout<<"extra large";
default:
cout<<"size not available";
}
return 0;
}
Q3/B
#include <iostream>
using namespace std;
int main() {
int A[3][4]={{3,4,6,1},{4,8,0,9},{5,7,1,2}};
int B[4][3];
for (int i=0; i<3;i++)
{
for (int j=0;j<4;j++)
{
B[j][i]=A[i][j]; // The main idea of the code
}
}
for(int i=0;i<4;i++)
{
for (int j=0;j<3;j++)
{
cout<<B[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Q4/
#include<iostream>
using namespace std;
int power(int base,int exponent) {
int ans=1;
for (int i=1;i<=exponent;i++) {
ans*=base;
}
return ans;
}
int fact(int n) {
int f=1;
for (int i=1;i<=n;i++) {
f*=i;
}
return f;
}
int main() {
double x,n,sum=0;
cin>>x>>n;
for (int i=0;i<n;i++) {
sum+=power(x,i)/fact(i);
}
cout<<sum;
}
Q5/
#include<iostream>
using namespace std;
int mostf(int a[],int s) {
int m=a[0];
int c=0;
for (int i=0;i<s;i++) {
int counter=0;
for (int j=0;j<s;j++) {
if (a[i]==a[j]) {
counter++;
}
}
if (counter>c) {
c=counter;
m=a[i];
}
}
return m;
}
int main() {
int ar[8]={1,6,3,6,3,4,6,5};
cout<<mostf(ar,8);
}
❤2