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

مشكلة في استخدام البوينتر

بدأه palestina في 5 يناير 2012 · 5 رد · 520 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

hi all

i have 2 classes , 1 of them use the other as below

the student class which use course class

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

#ifndef _STUDENT_H_

#define _STUDENT_H_

#include <iostream>

#include <string>

#include "Course.h"

using namespace std;

class Student

{

int id;

string name;

string matrixNo;

Course* course;

public:

Student();

Student(int, string, string);

void registerCourse(Course&);

};

#endif

course class below

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

#ifndef _COURSE_H_

#define _COURSE_H_

#include <iostream>

#include <string>

using namespace std;

class Course

{

int id;

string name;

string code;

public:

Course();

Course(int, string, string);

};

#endif

can u help me , i wanna know how to access the course which is used in student class ?

example i wanna print the course which registered by student .

1

2

3

4

5

6

7

8

9

10

11

12

13

#include <iostream>

#include "Course.h"

#include "Student.h"

using namespace std;

int main()

{

Student student1(1,"Ahmad","BIT1234");

Course course1(1,"Bachelor in IT","BIT");

student1.registerCourse(course1);

cout<<student1.id<<"\n"<<student1.name<<"\n"<<student1.matrixNo<<"\n"<<student1.course<<"\n";/////my problem here

system("PAUSE");

}

it print the address of course , not the value

#2

أخى تفضل هذا:

#ifndef _COURSE_H_
#define _COURSE_H_
#include <iostream>
#include <string>

using namespace std;
class Course
{
    int id;
    string name;
    string code;
public:
    Course();
    Course(int mid, string mname, string mcode)
    {
        id = mid;
        name = mname;
        code = mcode;
    }
    int GetID()    {        return id;    }
    string GetName (){ return name;}
    string Getcode() { return code;}
};

#endif

*****************************************************************************************************************************************************************

#ifndef _STUDENT_H_
#define _STUDENT_H_
#include <iostream>
#include <string>
#include "Course.h"

using namespace std;
class Student
{
    int id;
    string name;
    string matrixNo;
    Course *pcourse;
public:
    Student();
    Student(int mid, string mname, string mmatrixno)
    {
        id = mid;
        name = mname;
        matrixNo = matrixNo;
    }
    int GetID()    {        return id;    }
    string GetName (){ return name;}
    string GetmatrixNo() { return matrixNo;}

    void registerCourse(Course& c1)
    {
        pcourse = &c1;
    }
    Course *GetCourse()
    {
        return pcourse;
    }
};

#endif

************************************************************************************************************

#include <iostream>
#include <cstdlib>
#include "Student.h"
#include "Course.h"

using namespace std;
int main()
{
    Student student1(1,"Ahmad","BIT1234");
    Course course1(1,"Bachelor in IT","BIT");
    student1.registerCourse(course1);
    cout << student1.GetID() <<endl<<student1.GetName() <<endl<<student1.GetmatrixNo()<<endl;
    cout << course1.Getcode();
    system("PAUSE");
}

تحياتى العطرة والله الموفق

post-15367-027053900%201340345097.gifpost-15367-087183200%201340344597.gif
post-247365-0-10080400-1379403554_thumb.

#3
محمد ابوزايد كتب:

************************************************************************************************************


#include <cstdlib>
#include "Student.h"
#include "Course.h"

using namespace std;
int main()
{
Student student1(1,"Ahmad","BIT1234");
Course course1(1,"Bachelor in IT","BIT");
student1.registerCourse(course1);
cout << student1.GetID() <<endl<<student1.GetName() <<endl<<student1.GetmatrixNo()<<endl;
cout << course1.Getcode();
system("PAUSE");
}
#include <iostream>

تحياتى العطرة والله الموفق

شكرا جزيلا أخي محمد , حقيقة انا وجدت حلا أخر لعرض قيمة عناصر الكلاس course باستخدام student وهي كالتالي :


#include "Course.h"
#include "Student.h"

using namespace std;
int main()
{
Student student1(1,"Ahmad","BIT1234");
Course course1(1,"Bachelor in IT","BIT");
student1.registerCourse(course1);
cout<< student1.course->name;
system("PAUSE");
}
#include <iostream>

ربما تستطيع المساعدةفي توضيح اكثر للعلاقةinclude course هل هي تمثل الوراثة ؟؟؟

#4
اقتباس
ربما تستطيع المساعدةفي توضيح اكثر للعلاقةinclude course هل هي تمثل الوراثة ؟؟؟

ﻻ

post-15367-027053900%201340345097.gifpost-15367-087183200%201340344597.gif
post-247365-0-10080400-1379403554_thumb.

#5

أختى تفضلى الكود بعد التعديل:

لقد تم تبيق مفهوم الوراثة بالشكل المطلوب

وتمكنا من الوصول الى كل الدوال اﻻعضاء الخاصة بالفئة Course عن طريق الفئة المشتقة Student

وﻻحظ ايضا بانى الفئة الخاص بالفئة اﻻساسية تم استدعاءة عن طريق بانى الفئة المشتقة.

ﻻحظاننا دمجنا الملفات فى ملف واحد لسهولة العمل يمكنك تقسيمهما ولن يحدث اى مشاكل ان شاء الله

#include <iostream>
#include <cstdlib>

using namespace std;

class Course
{
protected:
    int id;
    string name;
    string code;

public:
    Course();
    Course(int cid, string cname, string ccode)
    {
        id = cid;
        name = cname;
        code = ccode;
    }
    int GetCourseID()
    {
        return id;
    }
    string GetCourseName ()
    {
        return name;
    }
    string GetCourseCode()
    {
        return code;
    }


}; /// end of Course

class Student : public Course
{
private:
    int id;
    string name;
    string matrixNo;

public:
    Student() : Course(){}
    Student(int sid, string sname, string smatrixNo,int cid, string cname, string ccode )
     : Course( cid, cname, ccode)
    {
        id = sid;
        name = sname;
        matrixNo = smatrixNo;
    }
    int GetStudentID()
    {
        return id;
    }
    string GetStudentName ()
    {
        return name;
    }
    string GetStudentmatrixNo()
    {
        return matrixNo;
    }

}; /// end of student class

int main()
{
    Student s1(1,"C++ Programming","A100",10,"Mohammed Abu Zayed", "Marix No(i don't now)");
    cout << "The course Data is: " << endl;
    cout << s1.GetCourseID()<< endl;
    cout << s1.GetCourseName()<< endl;
    cout << s1.GetCourseCode()<< endl;
    cout << "The Student Data is: " << endl;
    cout << s1.GetStudentID() << endl;
    cout << s1.GetStudentName() << endl;
    cout << s1.GetStudentmatrixNo() << endl;

    system("PAUSE");
    return 0;
}

تحياتى العطرة ... . والله الموفق.

post-15367-027053900%201340345097.gifpost-15367-087183200%201340344597.gif
post-247365-0-10080400-1379403554_thumb.

#6
اقتباس
وتمكنا من الوصول الى كل الدوال اﻻعضاء الخاصة بالفئة Course عن طريق الفئة المشتقة Student

الطالب قد يسجل نفسه فى اكثر من كورس او لا يسجل نفسه على الإطلاق، لهذا العلاقه بين الطالب و الكورس هي علاقة has-a و ليس is-a و بالتالي وراثة فئة الطالب لفئة الكورس ليست منطقيا خطأ و انما ايضا تصميم غير صحيح.

اقتباس
ربما تستطيع المساعدةفي توضيح اكثر للعلاقةinclude course هل هي تمثل الوراثة ؟؟؟

العلاقه بين فئة الكورس و فئة الطالب ليست و راثة و انما aggregation حيث ان الطالب قد يشير لأكثر من كورس و عند انتهاء الكائن المنشئ من فئة الطالب لن يقوم بتحرير الكائنات التى تم الإشاره إليها من فئة الكورس.

إذا قام الكائن من فئة الطالب بتحرير الذاكرة الخاصه بكائنات فئة الكورس - و هو امر المفترض ألا يحدث - حينها تكون العلاقة هي composition و ليس aggregation.

و الله ولي التوفيق

تم تعديل هذه المشاركة بواسطة محمد علاء الدين في 10 يناير 2012 في 16:47

مدونتي: C++ Tips and Tricks

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

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…