Oop Cyber
Oop Cyber
( البرمجة الشيئية – أو البرمجة غرضية التوجيه – البرمجة الموجه نحو الهدف – البرمجة الكائنية )
Class
Private Public
البرمجة الشيئية :هي أسلوب جديد للبرمجة من حيث شكل البرنامج ومن حيث وحدة بناء
البرنامج ومن حيث الخصائص الجديدة التي تسمح بها هذا األسلوب ،حيث يعتبر بناء البرنامج
هي الفئة ( )Classالتي تتكون من البيانات ومعها الدوال التي تعمل هذه البيانات .
Class ClassName
{
Private :
Private Data and Functions
Public :
Public Data and Functions
;}
طالب
الصفات :
02 أحمد
علوم 08
3
: اكتب برنامج يحسب حاصل جمع وضرب عددين
#include <iostream.h>
class num
{
private :
int a,b;
public :
void set_data(int x,int y)
{
a=x;
b=y;
}
void show_number()
{
cout<<"the sum of number = "<<a+b<<endl;
cout<<"the multiplication of number = "<<a*b<<endl;
}
}num1;
void main()
{
int n1,n2;
cin>>n1>>n2;
num1.set_data (n1,n2);
num1.show_number ();
}
5 4 النتائج
the sum of number = 9
the multiplication of number = 20
#include <iostream.h>
Press any key to continue
class circle
{
private :
float r;
double area1,circum1;
public :
void area(float r1)
{
r=r1;
area1=3.14*r*r;
cout<<"the area = "<<area1<<endl;
π = اكتب برنامج ليحسب مساحة ومحيط الدائرة علمًا }بأن المساحةclass باستخدام االصناف
void circum() π =3.14 π r 2= والمحيطr2
{
circum1=2*3.14*r;
cout<<"circle circum = "<<circum1<<endl;
}
};
void main()
{
circle ob1;
float n ;
cin>>n;
ob1.area(n); 4
ob1.circum ();
}
10 النتائج
the area = 314
circle circum = 62.8
Press any key to continue
5
: اكتب برنامج لحساب المثلث من العالقة التالية
حيث هي أضالعa,b,c
المثلث
#include <iostream.h>
#include <math.h>
class triangle
{
private :
float a,b,c,s,area;
public:
void set_data(float a1,float b1,float c1)
{
a=a1;
b=b1;
c=c1;
s=(a1+b1+c1)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
}
void show_data()
{
cout<<"the area of triangle = "<<area<<endl;
}
};
void main()
{
triangle ob1;
float n1,n2,n3;
10 10 10
cin>>n1>>n2>>n3; النتائج
the area of triangle = 43.3013
ob1.set_data(n1,n2,n3);
Press any
ob1.show_data ();key to continue
}
6
ليقرأ الزمن بالثواني ثم يطبع الزمن مقاسًا بالساعات والدقائق++C اكتب برنامج بلغة
. والثواني المتبقية
#include <iostream.h>
class timer
{
private :
int s,m,h;
public:
void time (int x)
{
s=x;
m=h=0;
while (x>=60)
{
m=m+1;
x=x-60;
}
while (m>=60)
{
h=h+1;
m=m-60;
}
cout<<h<<":"<<m<<":"<<x<<endl;
}
}ob1;
void main()
{
int n1;
cin>>n1;
3735ob1.time (n1); النتائج
} 1:2:15
Press any key to continue
7
دوال البناء ()Construction Functions
هي دوال تحمل اسم الفصيلة وتستخدم في إعطاء قيم ابتدائية لبيانات الفصيلة أو تنفيذ مجموعة
سطور ،حيث أنها تنفذ تلقائيًا بمجرد إعالن هدف للفصيلة .ومثال على ذلك البرنامج التالي :
>#include <iostream.h
class myclass
{
private:
;int a,b
public:
)myclass (int i,int j
{
;a=i
;b=j
}
)(void show_data
{
;cout<<"a= "<<a<<"\t"<<"b= "<<b<<endl
}
;}
)(void main
{
;)myclass ob1(5,7
;)( ob1.show_data
}
8
نفس البرنامج فقط هذا التعديل الموضح أدناه
)myclass (int i=5,int j=7
{
;a=i
;b=j
}
)(void show_data
{
;cout<<"a= "<<a<<"\t"<<"b= "<<b<<endl
}
;}
)(void main
{
;)myclass ob1(15,70
;)( ob1.show_data
;myclass ob2
;)( ob2.show_data
}
10
#include <iostream.h>
class myclass
{
private :
int a,b,sum;
public :
myclass ()
{
a=5;
b=7;
}
myclass(int i,int j)
{
a=i;
b=j;
sum=a+b;
cout<<"the sum = "<< sum<<endl;
}
void show_data()
{
cout<<"a="<<a<<"\t"<<"b="<<b<<endl;
}
};
void main()
{
myclass ob1;
myclass ob2(8,8);
ob2.show_data();
}
11
: مثال آخر
#include <iostream.h>
class counter
{
private:
int i;
double d;
char c;
public :
counter(int a)
{
i=a;
cout<<"i="<<++i<<endl;
}
counter(double e)
{
d=e;
cout<<"d="<<++d<<endl;
}
counter(char h)
{
c=h;
cout<<"c="<<++c<<endl;
}
};
void main()
{ i=6 النتائج
d=7.4
counter ob1(5);
c=b ob2(6.4);
counter
Press
counter any key to continue
ob3('a');
}
12
اكتب برنامج باستخدام دوال فائقة التحميل إليجاد مساحة المثلث: مثال
حيث هي أضالعa,b,c
المثلث
# include <iostream.h>
#include <math.h>
class triangle
{
private :
float a,b,c;
double area1,s;
public :
triangle()
{
a=5;
b=5;
c=5;
s=(a+b+c)/2;
area1=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"area="<<area1<<endl;
}
triangle( float x,float y,float z)
{
a=x;
b=y;
c=z;
s=(a+b+c)/2;
area1=sqrt(s*(s-a)*(s-b)*(s-c));
}
void show_data()
{
cout<<"area="<<area1<<endl;
}
};
void main()
{
triangle ob1;
triangle ob2 (10,10,10);
ob2.show_data();
}
area=10.8253 النتائ
area=43.3013 ج
Press any key to continue
13
: Destructor Function دوال الهدم والمدمرات
وتنفذ عند الخروج من الفصيلة أو عند االنتهاء من استعمال الهدف، توجد دالة الهدم في الفصيلة
ويستفاد ومن هذه الدالة عندما نريد تنفيذ بعض السطور عند االستخدام من. المعلن من الفصيلة
الهدف وغالبًا مات ستخدم دالة الهدم في حذف المتغيرات من الذاكرة ولتوضيح استخدام دالة
-: الهدم نستخدم المثال التالي وهو إيجاد مساحة المثلث
#include <iostream.h>
#include<math.h>
class triangle
{
private :
float a,b,c;
double s;
public :
triangle (float x,float y,float z)
{
a=x;
b=y;
c=z;
s=(a+b+c)/2;
}
void area()
{
cout<<"area="<<sqrt(s*(s-a)*(s-b)*(s-c));
#include <iostream.h>
cout<<endl;
class distance
}
{ ~triangle()
private : {
double feet,inch;
cout<<"Destructor "<<"a="<<a<<"\t"<<"b="<<b<<"\
public t"<<"c="<<c<<endl;
:
distance(float
} x)
{ };
voidinch=x;
main() area=43.3013 النتائج
{ feet=0;
Destructor a=10 b=10 c=10
} triangle ob1(10,10,10);
Press any key to continue
void dis()
ob1.area ();
{}
while (inch>=12)
{
inch-=12;
feet+=1;
}
cout<<"feet="<<feet<<"\t"<<"inch="<<inch<<endl;
12= القدم
} ليقرأ المسافة بالبوصات ثم يطبع الناتج باألقدام علمًا بأن++C اكتب برنامج بلغة
~distance() . وذلك باستخدام دالة الهدم. بوصة
{
cout<<"Destructor"<<"\t"<<"inch="<<inch<<"\
t"<<"feet="<<feet<<endl;
}
};
void main()
{
int n1; 25 النتائج
cin>>n1; feet=2 inch=1
distance Destructor
ob1(n1); inch=1 feet=2
ob1.dis(); 14
Press any key to continue
} inch=inch-12;
feet=feet+1;
أنشئ فصيلة لتقرأ المسافة مقاسة بالسنتبمترات ثم تطبع تلك المسافة باألقدام
.والبوصات والسنتيمترات المتبقية مستخدما في ذلك دالتي البناء والهدم
#include<iostream.h>
class distance
{
private:
float cm,inch,feet;
public:
distance(float x)
{
cm=x;
inch=feet=0;
while(cm>=2.5)
{
inch++;
cm-=2.5;
}
while(inch>=12)
{
inch-=12;
feet++;
}
cout<<feet<<" :"<<inch<<" : "<<cm<<endl;
}
~distance()
{
cout<<"destructing \n";
}
};
void main()
{
float n;
cin>>n;
distance d1(n);
}
17
باستخدام المصفوفاتo
#include <iostream.h>
#define max 100
class stack
{
private :
int st[max];
int top;
public :
stack()
{
top=0;
}
void push(int a)
{
st[++top]=a;
}
int pop()
{
return st[top--];
}
};
void main()
{
stack s1;
int t[5];
int i,j;
for (i=1;i<=5;i++)
{
cin>>t[i];
s1.push(t[i]);
}
for (j=1;j<=5;j++)
cout<<j<<":"<<s1.pop()<<endl;
}
18