الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مشكلة في التعامل مع ال Objects

مغلق
بدأه Wase3 Sadr في 8 مارس 2005 · 7 رد · 724 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ..

إخواني انا عندي بربلم يقوم بجمع وطرح الـ Complex Numbers .. وهذا تطبيقي عليه :

#include <iostream>
using namespace std;

class Complex
{
	double realPart , imaginaryPart; 

public :
	Complex ();
	Complex add (const Complex &c1);
	Complex sub (const Complex &c2);
	void set (double x , double y );
	void print ();
	void printAdd ();
	void printSub ();
}


Complex::Complex ()
{
	realPart = 0.0;
	imaginaryPart =0.0;
}


Complex Complex::add(const Complex &c1)
{
	Complex t;
	double a , b;
	a = realPart + c1.realPart;
	b = imaginaryPart + c1.imaginaryPart;

	t.set (a , b );
	return t;
}

Complex Complex::sub (const Complex &c2)
{
	Complex t;
	double a , b;
	a = realPart - c2.realPart;
	b = imaginaryPart - c2.imaginaryPart;

	t.set (a , b );
	return t;
}

void Complex::printAdd()
{
	cout << realPart << "+" << imaginaryPart << "i" <<endl;
}

void Complex::printSub()
{
	cout<<realPart << "-" << imaginaryPart <<"i"<<endl;

}

void Complex::print()
{
	cout << "Add=" << printAdd() <<endl;
	cout << "Sub=" << printSub() <<endl;
}


int main()
{
	Complex c1 , c2 , c3;

	double a , b , c , d;

	cout << "Enter First Complex Number :";
	cin >> a >> b;

	c1.set(a , b );

	cout << "Enter Second Complex Number :";
	cin >> c >> d;

	c2.set (c , d );

	c3 = c1 .add (c2);
	c3 = c1 .sub (c2);

	c3.print();

	return 0;
}

لكن المشكلة تطلع لي هذي الأخطاء !! :

--------------------Configuration: Complex - Win32 Debug--------------------
Compiling...
Complex.cpp
D:\Unevirsty\Csc113\6.6\Complex.cpp(20) : error C2533: 'Complex::Complex' : constructors not allowed a return type
D:\Unevirsty\Csc113\6.6\Complex.cpp(28) : error C2264: 'Complex::Complex' : error in function definition or declaration; function not called
D:\Unevirsty\Csc113\6.6\Complex.cpp(39) : error C2264: 'Complex::Complex' : error in function definition or declaration; function not called
D:\Unevirsty\Csc113\6.6\Complex.cpp(61) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
D:\Unevirsty\Csc113\6.6\Complex.cpp(62) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
D:\Unevirsty\Csc113\6.6\Complex.cpp(68) : error C2264: 'Complex::Complex' : error in function definition or declaration; function not called
D:\Unevirsty\Csc113\6.6\Complex.cpp(68) : error C2264: 'Complex::Complex' : error in function definition or declaration; function not called
D:\Unevirsty\Csc113\6.6\Complex.cpp(68) : error C2264: 'Complex::Complex' : error in function definition or declaration; function not called
Error executing cl.exe.

Complex.obj - 8 error(s), 0 warning(s)

ياليت إذا تقدرون تساعدوني وتفهموني سبب هالأخطاء !!

Wase3 Sadr

#2

you forgot the semicolon ';' after the class definition

class a
{

};

sorry for typing english, I didn't figure out yet how to type arabic from the university's machine running Fedora Core 1

#3

return t;=======================> constrocter can not return value

#4

هلا اخوي

انا قريت الكود على السريع لقيت اخطاء بسيطة إن شاء الله اول ماافضى ارجع لك بالحل الكامل بإذن الله

#5

السلام عليكم

بعد المرور السريع إكتشفت انك لم تعرف الداله complex::set وكما ذكر قبلى الاخ حسام عليك ان تضع ; فى نهاية تعريف الclass:

بعض النصائح البسيطه:

إستخدم operation overload للعلامات + و - بدل من sub و add.

وبدل من إستخدام print يمكنك إستخدام operation overload ل << و>> ;

والسلام

لا إله إلا الله محمد رسول الله

busbar : يجب ان تدرك انه هناك حد ادنى للمعرفة المطلوبة قبل البدء في عمل أي شئ.

#6

ماهو operation overload ؟

وكيف أستخدمه !!

#7

السلام عليكم

دعنا نقوم بتحليل كامل للبرنامج:

class Complex
{
double realPart , imaginaryPart;

public :
Complex ();
Complex add (const Complex &c1);
Complex sub (const Complex &c2);
void set (double x , double y );
void print ();
void printAdd ();
void printSub ();
};

هذا هو الكود التى يقوم بتعريف class complex, دعنا نمر عليه خطوه بخطوه

الجزئيه الاولى مظبوطه وهى وضع الارقام فى private لانك لاتريد ان يصل إليها المستخدم بطريقه مباشره.

class Complex
{
double realPart , imaginaryPart;

public :

والان نحتاج constructure, وفيه نقوم بإعطاء القيم الاوليه اولاً, والان بدلاً من كتابته بالطريقه التى قمت بها نستخدم initializing list على النحو التالى:

class Complex
{
double realPart , imaginaryPart;

public :
Complex ():realPart(0.0),imaginaryPart(0.0)){ }

بإستخادمنا ل intializing list نقلص حجم البرنامج ونزيد سرعة ادائه, والان نحتاج إلى 3 constructer بالاضافه لل default constructor الذى قمنا بعمله, وذالك إذا احب المستخدم ان يضع القيم مباشرتاً على النحو التالى :

complex a= complex(1,2);

بدلاً من وضعها عن طريق إستدعاء داله. ولكن بدلاٌ من كتابة 4 constructure يمكننا دمجها فى constructure واحد على النحو التالى

class Complex
{
double realPart , imaginaryPart;

public :
Complex (double r=0.0,double i=0,0 ):realPart(r),imaginaryPart(i)){ }

والان لا تحتاج للداله set لان القيم ستوضع فى ال object خلال الانشاء.

والان operation overload ماذا يحدث إذا كان لديك المعادله التاليه وفيها جميع العناصر من نوع int.

a=b+c

بالتاكيد قيمة a يتصبح حاصل الجمع, ولكن ماذا يحدث إذا كانت الاعداد complex ؟؟

هنا وجب علينا بتعريف معنى المعادله للسى بلس بلس, وهناك 4 حالات على النحو التالى:

complex operator+(complex x,complex y);
complex operator+(complex x,double y);
complex operator+(double x,complex y);
complex operator+(double x,double y);

هذه الطريقه هى اسهل طريقه للشرح ولكن هناك طرق اخرى افضل, فمثلا يمكنك كتابة داله تحول double الى complex قبل القيام بعملية الجمع ولكن دعنا نشرح هذه الطريقه الاول.

ساقوم بكتابة الداله الاولى وباقى الدوال متشابهه ويمكنك إذا فهمت الطريقه ان تقوم بكتابة بقية الدوال.

complex operator+(complex x,complex y){
realPart=x.realPart + y.realPart;
imagnaryPart=x.imagnaryPart+y.imagenaryPart;
return this;
}

هناك الكثير فى هذه الماده ولايسعنى ذكر كل شئ ولكن ارجو ان تكون الصوره واضحه, إقراء عن operation overloading فى اى كتاب للسى بلس بلس, وإذا كان لديك اى سؤال اطرحه فى المنتدى وإن شاء الله الاخوه جميعاً يشاركو فى الاجابه .

والسلام

لا إله إلا الله محمد رسول الله

busbar : يجب ان تدرك انه هناك حد ادنى للمعرفة المطلوبة قبل البدء في عمل أي شئ.

#8

الله يعطيك العافيه .. معلوماتك وسعّت مداركي :)

بأقرا ان شاء الله عنه :) ..

ألف ألف شكر ..

هذا الموضوع مغلق.

مواضيع مشابهة