0% found this document useful (0 votes)
62 views17 pages

OOP امثاله

The document contains examples of C++ programs demonstrating various OOP concepts like classes, objects, methods, constructors, destructors, friend functions, static functions, and const objects. Some key examples include: 1. Defining classes for mobile phones and vehicles with member variables and methods to set/get prices, speeds, and areas. 2. Using constructors and destructors to initialize values and print outputs. 3. Demonstrating passing objects to methods and returning new objects. 4. Using friend functions to access private members of different classes. 5. Defining static and const qualifiers on class members and methods. The examples provide code snippets to explain concepts

Uploaded by

Mohammed -Jasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views17 pages

OOP امثاله

The document contains examples of C++ programs demonstrating various OOP concepts like classes, objects, methods, constructors, destructors, friend functions, static functions, and const objects. Some key examples include: 1. Defining classes for mobile phones and vehicles with member variables and methods to set/get prices, speeds, and areas. 2. Using constructors and destructors to initialize values and print outputs. 3. Demonstrating passing objects to methods and returning new objects. 4. Using friend functions to access private members of different classes. 5. Defining static and const qualifiers on class members and methods. The examples provide code snippets to explain concepts

Uploaded by

Mohammed -Jasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 17

OPP set and get and print ‫مثال عن موضوع سيت وكيت عن شراء موبايل في طريق‬

#include<iostream>
using namespace std;

class mobile {

private:
int price;
string gh;
string mm;
public:

void setprice(int x)
{
if (x > 800)
cout << "We do not have this mobile price\n";

else if (x == 799)
price = 799;
else if (x < 0)
//‫الدين ممنوع رجائن‬
cout << "no mobile \n";
else
price = x;

}
int get_price() {
return price;
}

void print_price() {
cout << price << '$' << endl;
}

};
int main() {

mobile tcno, iphone;

int n;
cout << "enter mane mobile : \n";

cin >> n;

tcno.setprice(n);

cout << tcno.get_price() * 3<<'$';


}
-----------------------------------------------------------------------------------
--------------------------------------------------------

#include<iostream>
using namespace std;

class mobile {

private:
int price;
string gh;
string mm;
public:

void setprice(int x)
{
if (x > 800)
cout <<"We do not have this mobile price\n";

else if (x==799)
price = 799;
else if (x < 0)
//‫الدين ممنوع رجائن‬
cout << "no mobile \n";
else
price = x;

int get_price(int m) {

if (m<4)
return price*m;
else

{
cout <<"We do not sell this number of mobile phones. We can only sell
one or 2 mobile phones\n";
return price;
}
}}

void print_price() {
cout << price << '$'<<endl;
}

};
int main() {
mobile tcno, iphone;

int n,c;
cout << "enter mane mobile n : \n";

cin >> n;
cout << "enter mane mobile c : \n";
cin>>c;
tcno.setprice(n);

cout <<tcno.get_price(c)<<'$' ;

}
-----------------------------------------------------------------------------------
--------------------------------------------------------

‫في سعرة من خلل تمرير اكثر من اوبجكت للدوال الممبر والنون ممبر‬ ‫مثال عن مقارنه جهاز‬

#include <iostream>

using namespace std;


class mobile {
private : string company ;
int price;
public:
void set (string a,int c);//{company=a;price=c;}

int get_price(){
return price;}

bool check(mobile);

};
void mobile ::set (string a,int c){company=a;price=c;}

bool mobile ::check(mobile t){


if(price==t.price)
return true ;
return false;

bool check2(mobile w,mobile e){


if(w.get_price()== e.get_price())
return true ;
return false;}

int main()
{mobile m1,m2,m3,m4;

m1.set ("lg",500);
m2.set("hp",500);
if (check2(m1,m2))
cout <<"samd mobile ok ";
else
cout <<"not same mobile dl ";

cout <<"\n--------------------------------------------------------\n";
m3.set ("dell",800);
m4.set("max",500);
if (m3.check(m4))
cout <<"samd mobile ok ";
else
cout <<"not same mobile dl ";

}
-----------------------------------------------------------------------------------
--------------------------------------------------------

‫جمع اوبجكتين ووضعه في اوبجكت جديد‬+++ ‫برنامج ارجاع بيانات من نوع اوبجكت‬

#include <iostream>

using namespace std;


class mobile {
private : string company ;
int price;
public:
void set (string a,int c){company=a;price=c;}

int get_price(){
return price;}

mobile sum(mobile x){


mobile k;

k.price= price+x.price;

return k;

};

int main(){
mobile m1,m2,m3;
m1.set("gf",700);
m2.set("ng",300);

m3=m1.sum(m2);
cout <<m3.get_price();

cout <<"\n---------------------------------------------------\n";

m3.set("gf",700);
m2.set("ng",800);

m1=m3.sum(m2);

cout <<m1.get_price();

}
-----------------------------------------------------------------------------------
--------------------------------------------------------

‫عمل كالسين وجمع الكالسين‬ ‫التعامل مع اكثر من كالس‬

#include <iostream>

using namespace std;

class square {
private :
int le;
public:
void set_le(int x){le=x;}
int sq_area(){return le*le;}

//double sum_sq1_ci1(circle b){


//return sq_area()+ b.ci_area();}

};

class circle{
private :
int r;
public:
void set_r(int y){r=y;}
double ci_area(){return r*r*3.14;}

double sum_sq_ci(square a){


return ci_area()+ a.sq_area();

}
};

double sum(square z,circle w){

return z.sq_area()+w.ci_area();
}

int main (){


int d,f;
cout <<"enter the number d and f\n";
cin>>d>>f;
square sd;

circle po;

po.set_r(d);
sd.set_le(f);

cout << po.sum_sq_ci(sd);


cout<<"\n---------------------------------\n";
cout <<"enter the number d and f\n";
cin>>d>>f;
po.set_r(d);
sd.set_le(f);

cout <<sum(sd,po);

-----------------------------------------------------------------------------------
------------------------------------------------------

‫برنامج التعامل مع مصفوفة مع اوبجكت كالس‬

#include <iostream>

using namespace std;

class car {
private :
int speed;
public:
void go(){speed=20;}
void speedup(){speed=90;}
void speedc(){speed=190;}
void stop(){speed=0;}
int get_speed(){return speed;}};

int main (){


car cr[10];
for (int i=0;i<10;i++){
cr[i].go();
cout <<"car "<<i<<" speed "<<cr[i].get_speed()<<"\n";}

cr[3].stop();
cr[6].speedup();
cr[9].speedc();
cr[10].stop();
cr[2].speedup();
cr[1].speedc();
cr[2].speedup();
cout <<"\n\n\nafter chang \n";
for (int i=0;i<10;i++){

cout <<"car "<<i<<" speed "<<cr[i].get_speed()<<"\n";}

-----------------------------------------------------------------------------------
-------------------------------------------------------

‫هو موضع الكونستركتور‬ ‫ موضوع كلش مهم‬constructor

‫البراميتر ازد كونستركتور‬

#include <iostream>

using namespace std;

class car {
private :
int w,r;
public:

car(int a,int y){w=a;r=y;}

int area()
{
return (w*r)-r+w;
}};

int main (){


car s(4,5);

cout << s.area();


}

-----------------------------------------------------------------------------------
---------------------------------------------------------

#include <iostream>
using namespace std;

class car {
private :
string com ;
int w;
public:

car(){com="no company ";


w=0;}

car(int x){
com="no company ";
w=x;

}
car (string x){
com=x;
w=0;
}

car (string x,int s){

com=x;
w=s;

void print (){

cout <<"company : " <<com<<endl;


cout <<"speed : "<< w;
}

};

int main (){


car cq;

cq.print();
cout <<"\n";

car q(56);

q.print();
cout <<"\n";

car c("vvvnb");
c.print();
cout <<"\n";

car cqc("vvv",99);

cqc.print();
}

-----------------------------------------------------------------------------------
----------------------------------------------------------
‫ برانمج عن كوضوع‬destructor

‫برنامج يجمع عددين وارجاع قيمتها في نهاية البرنامج يتمه طباعت الناتج‬

#include <iostream>
using namespace std;

class rect
{
private:
int a,c ,n;
public:
rect(int m,int v) {
c = m; a = v;

n= c + a;

cout << c<<"+"<<a<<"=";

}
int get_n(){return n;}
~rect() {
cout << "\nfind sum c+a = " << get_n()<<endl;

};

int main() {
rect m(8,9);
cout << "\ninside program \n";
cout << "\ninside program \n";
cout << "\ninside program \n";
cout << "\ninside program \n";

rect v(5,4);

cout << "\n---------------------------------------------------\n";


}
-----------------------------------------------------------------------------------
--------------------------------------------------------
destructor ‫مثال اخر يوضح عمل موضع‬

#include <iostream>
using namespace std;

class rect
{
private:
int a,c ,n;
public:
rect(int m,int v) {
c = m; a = v;

n= c + a;

cout <<"constructor : "<< c<<"+"<<a<<"=";

}
int get_n(){return n;}
~rect() {
cout <<"destructor : "<< c<<"+"<<a<<"=" << get_n()<<endl;

};
rect c (5,5);

int main() {
cout << "\n---------------------------------------------------\n";

rect m(8,9);
cout << "\ninside program \n";
rect v(5,4);
cout << "\n---------------------------------------------------\n";

if(4<5){rect h (4,4);
cout <<"\n";}
cout << "\n---------------------------------------------------\n";

rect x(2,3);
cout << "\n---------------------------------------------------\n";

-----------------------------------------------------------------------------------
-------------------------------------------------------

‫ برنامج يوضح كيف يعمل موضوع الفريند فنكشن‬friend


#include <iostream>
class mob;
using namespace std;

class lap
{
private:
int price ,ram;string n;
public:
lap(string x,int c,int a){

n=x; price=c; ram=a;

}
friend total_price(lap,mob);
};

class mob
{
private:
int price ,ram;string n;
public:
mob(string b,int m,int v){

n=b; price=m; ram=v;

}
friend total_price(lap,mob);
};

int total_price(lap a,mob c){

return a.price+c.price;

int main() {

lap bn("dell",500,8);
mob fd("appal",1000,256);

cout <<total_price(bn,fd);

-----------------------------------------------------------------------------------
---------------------------------------------------------
‫ برنامج عن دوال الستاتك هو ايضا يمكن تمرير من خللو دال من اوبجكت ثابت‬static and const
#include <iostream>

using namespace std;


class car {
private :
int speed ;
string com;
int modm;
public :
car (string c,int s,int m){

com=c; speed=s;modm=m;

}
static void fun() {

cout <<"car enter ldfjr :\n";

}
};
int main (){

const car c("kkd",130,2019);


c.fun();

car::fun();

-----------------------------------------------------------------------------------
-------------------------------------------------------

‫يعني اليمكن تغير اي متغير في االوبجكت‬ ‫برنامج عن الكونستانت اوبجكت‬

#include <iostream>

using namespace std;


class car {
private :
int speed ;
string com;
int modm;
public :
car (string c,int s,int m){

com=c; speed=s;modm=m;

}
void fun() const {

cout <<"car enter ldfjr :\n";


}
};
int main (){

const car c("kkd",130,2019);


c.fun();

}
-----------------------------------------------------------------------------------
----------------------------------------------------------

‫برنامج كالس داخل كالس‬

#include <iostream>

using namespace std;


class car {
private :
int speed ;
string com;
int modm;
public :
class samnd{
public:
int u;

};

void foo(){
samnd sa;

cin>>sa.u;

cout <<sa.u;

}
};
int main (){
car ff;

ff.foo();

}
-----------------------------------------------------------------------------------
----------------------------------------------------------

‫بيوت‬ ‫برانامج‬
#include <iostream>
using namespace std;

class room {
int h, w, l;

public :
void set_room() {
cout << "enter number to h and w and l";
cin >> h >> w >> l;
}
void print(){
cout << "find h : " << h << " -find w : " << w<<" -find l : " << l<<endl;
}
};
class adriss { int house_num;
char city[30];
char state[30];
public:

void adr() {
cout << "house number : ";
cin >> house_num;
cout << "city : ";
cin >> city;
cout << "state: ";
cin >> state;

}
void putad()
{
cout << "House No.: " << house_num << ",city: " << city << ",state: "
<< state << endl;
}

};
class house {
char house_name[30];

adriss ss;
room rr[10];
public:

void inbut();
void display();
};

void house::inbut() {
cout << "enter house name";
cin >> house_name;
cout << "enter adriss\n";
ss.adr();

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


cout << "house diand " << i + 1 << "\n";
rr[i].set_room();

cout <<"\n-------------------------\n";
}
}
void house::display() {
cout << "huose name :" << house_name;
cout <<" \n adriss is ";

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


cout << "house detalis :" << i + 1 << "\n";
rr[i].print();

cout <<"\n-------------------------\n";
}

int main ()
{
house as;

as.inbut();

as.display();

===================================================================================
=========================================================
‫التعامل مع اكثر من اوبجكنت ب فكشن واحد‬

#include <iostream>

using namespace std;

class ddf{

private:
int s,a,d;
public:

void set_s_a_d(int w,int f,int c){s=w;a=f;d=c;}

int get(){

int cc;
if (s==a){
cc= s*a*d;
cout <<" find s*a*d= "<<cc;
return cc;}

else
cc= s*a;
cout <<" find s*a*= "<<cc;
return cc;}

bool cheak(ddf c){if (s==c.s&&a==c.a&&d==c.d)return true ;return false;}

};

int main()
{ ddf aa1,aa3;int a,s,d;

int vv, xx, cc;

cout <<"enter the number to a and s and d\n" ;

cin>>a>>s>>d;

aa1.set_s_a_d(a,s,d);

cout <<"enter the number to a and s and d\n" ;

cin>>a>>s>>d;

aa3.set_s_a_d(a,s,d);

if (aa1.cheak(aa3)){

xx= aa1.get();cc =aa3.get();

vv=xx+cc;

cout <<"\n \n fine aa1+aa3 ="<<vv;}

else

cout <<"dkkkkkkkk";

}
-----------------------------------------------------------------------------------
--------------------------------------------------------
‫جمع اوبجكتين ووضعها في اوبجكت جديد‬ ‫برنامج ارجاع بيانات كالس‬

#include <iostream>

using namespace std;

class ddf {

private:
int s, a, hh,sdd;
public:

void set_s_a_d(int w, int f, int c) { s = w; a = f; hh= c; }

ddf sum(ddf d) {

ddf r;

int sdd;
r.sdd = s + d.s+a + d.a+hh+ d.hh ;

return r;
}
int get() { return sdd; }
};

int main()
{

ddf as, ad, af;

as.set_s_a_d(8,4,3);
ad.set_s_a_d(2,3,4);

af = as.sum(ad);
cout << "skdkdf "<<af.get();

You might also like