nurul_h5 0 Report post Posted March 22, 2011 atucara ni digunakan untuk bagi gred markah n kira purata. ada error dan pada masa yang sama kena masuk polymorphism. camner ek????????aku dh buntu ni.........btw thanks for your help #include<iostream> using namespace std; class Teacher { protected: char stafname[25],stafid[10]; public: void accept() { cout<<"\nEnter your name :"<<endl; cin>>stafname; cout<<"\nEnter staffid :"<<endl; cin>>stafid; } }; class Student:public Teacher { private: char Class[3],subject[12],name[25]; int a,bil,mark_bm1[ ],mark_bm2[ ],mark_math[ ],mark_science[ ],mark_bi[ ],total_bm1,total_bm2,total_math,total_science,total_bi; int average_bm1,average_bm2,average_math,average_science,average_bi; public: void Accept(); void display(); }; void Student::Accept() { cout<<"\nEnter Class :"; cin>>Class; cout<<"\nEnter number of student :"; cin>>bil; for(a=0;a<bil;a++) { cout<<"\nEnter student name :"; cin>>name; cout<<"\nEnter mark for Bahasa Melayu 1 :"; cin>>mark_bm1[a]; cout<<"\nEnter mark for Bahasa Melayu 2 :"; cin>>mark_bm2[a]; cout<<"\nEnter mark for Mathematics :"; cin>>mark_math[a]; cout<<"\nEnter mark for Science:"; cin>>mark_science[a]; cout<<"\nEnter mark for English:"; cin>>mark_bi[a]; total_bm1=total_bm1+mark_bm1[a]; total_bm2=total_bm2+mark_bm2[a]; total_math=total_math+mark_math[a]; total_science=total_science+mark_science[a]; total_bi=total_bi+mark_bi[a]; average_bm1=total_bm1/bil; average_bm2=total_bm2/bil; average_math=total_math/bil; average_science=total_science/bil; average_bi=total_bi/bil; } } void Student::display() { cout<<"\nClass :"<<Class; cout<<"\t\tBil. Student :"<<bil; for( a=0;a=bil;a++) { if(mark_bm1[a]>=80) { cout<<"Name :"<<name; cout<<"\n\nBahasa Melayu 1 :"<<mark_bm1[a]; cout<<"\tGred A"; } else if(mark_bm1[a]>59||mark_bm1[a]<80) { cout<<"Name :"<<name; cout<<"\n\nBahasa Melayu 1 :"<<mark_bm1[a]; cout<<"\tGred B"; } else if(mark_bm1[a]>39||mark_bm1[a]<60) { cout<<"Name :"<<name; cout<<"\n\nBahasa Melayu 1 :"<<mark_bm1[a]; cout<<"\tGred C"; } else { cout<<"Name :"<<name; cout<<"\n\nBahasa Melayu 1 :"<<mark_bm1[a]; cout<<"\tGred D"; } if(mark_bm2[a]>=80) { cout<<"\n\nBahasa Melayu 2 :"<<mark_bm2[a]; cout<<"\tGred A"; } else if(mark_bm2[a]>59||mark_bm2[a]<80) { cout<<"\n\nBahasa Melayu 2 :"<<mark_bm2[a]; cout<<"\tGred B"; } else if(mark_bm2[a]>39||mark_bm2[a]<60) { cout<<"\n\nBahasa Melayu 2 :"<<mark_bm2[a]; cout<<"\tGred C"; } else { cout<<"\n\nBahasa Melayu 2 :"<<mark_bm2[a]; cout<<"\tGred D"; } if(mark_math[a]>=80) { cout<<"\n\nMathematics :"<<mark_math[a]; cout<<"\tGred A"; } else if(mark_math[a]>59||mark_math[a]<80) { cout<<"\n\nMathematics :"<<mark_math[a]; cout<<"\tGred B"; } else if(mark_math[a]>39||mark_math[a]<60) { cout<<"\n\nMathematics :"<<mark_math[a]; cout<<"\tGred C"; } else { cout<<"\n\nMathematics :"<<mark_math[a]; cout<<"\tGred D"; } if(mark_science[a]>=80) { cout<<"\n\nScience :"<<mark_science[a]; cout<<"\tGred A"; } else if(mark_science[a]>59||mark_science[a]<80) { cout<<"\n\nScience :"<<mark_science[a]; cout<<"\tGred B"; } else if(mark_science[a]>39||mark_science[a]<60) { cout<<"\n\nScience :"<<mark_science[a]; cout<<"\tGred C"; } else { cout<<"\n\nScience :"<<mark_science[a]; cout<<"\tGred D"; } if(mark_bi[a]>=80) { cout<<"\n\nBahasa Inggeris :"<<mark_bi[a]; cout<<"\tGred A"; } else if(mark_bi[a]>59||mark_bi[a]<80) { cout<<"\n\nBahasa Inggeris :"<<mark_bi[a]; cout<<"\tGred B"; } else if(mark_bi[a]>39||mark_bi[a]<59) { cout<<"\n\nBahasa Inggeris :"<<mark_bi[a]; cout<<"\tGred C"; } else { cout<<"\n\nBahasa Inggeris :"<<mark_bi[a]; cout<<"\tGred D"; } } cout<<"\n Average for Bahasa Melayu 1:"<<average_bm1; cout<<"\n Average for Bahasa Melayu 2:"<<average_bm2; cout<<"\n Average for Mathematics:"<<average_math; cout<<"\n Average for Science:"<<average_science; cout<<"\n Average for English is:"<<average_bi; } void main() { Student obj1; obj1.accept(); obj1.Accept(); obj1.display(); } Quote Share this post Link to post Share on other sites
syahmixp 44 Report post Posted March 22, 2011 Rasa musykil bila tengok kod... [code] int a,bil,mark_bm1[ ],mark_bm2[ ],mark_math[ ],mark_science[ ],mark_bi[ ],total_bm1,total_bm2,total_math,total_science,total_bi;[/code] mungkin sebab array mark_bm1, mark_bm2, dan lain2 diisytihar tanpa saiz. Quote Share this post Link to post Share on other sites
nurul_h5 0 Report post Posted March 22, 2011 [quote name='syahmixp' timestamp='1300778205' post='1065408'] Rasa musykil bila tengok kod... [code] int a,bil,mark_bm1[ ],mark_bm2[ ],mark_math[ ],mark_science[ ],mark_bi[ ],total_bm1,total_bm2,total_math,total_science,total_bi;[/code] mungkin sebab array mark_bm1, mark_bm2, dan lain2 diisytihar tanpa saiz. [/quote] maybe..........tapi aku nk wt array yg infinate...........ade cara ke??????? Quote Share this post Link to post Share on other sites
syahmixp 44 Report post Posted March 22, 2011 (edited) tak boleh, array mesti ada saiz. sila tengok malloc, realloc dan free [url="http://www.cplusplus.com/reference/clibrary/cstdlib/malloc/"]http://www.cplusplus...cstdlib/malloc/[/url] ubah mark_*... menjadi integer pointer [code]int* mark_bm1, mark_bm2, mark_math ...[/code] sebelum [code] for(a=0;a<bil;a++)[/code] letak kod ini untuk allocate memory [code]mark_bm1 = (int)malloc(sizeof(int)*bil);[/code] Edited March 22, 2011 by syahmixp Quote Share this post Link to post Share on other sites
nurul_h5 0 Report post Posted March 26, 2011 [quote name='syahmixp' timestamp='1300783519' post='1065416'] tak boleh, array mesti ada saiz. sila tengok malloc, realloc dan free [url="http://www.cplusplus.com/reference/clibrary/cstdlib/malloc/"]http://www.cplusplus...cstdlib/malloc/[/url] ubah mark_*... menjadi integer pointer [code]int* mark_bm1, mark_bm2, mark_math ...[/code] sebelum [code] for(a=0;a<bil;a++)[/code] letak kod ini untuk allocate memory [code]mark_bm1 = (int)malloc(sizeof(int)*bil);[/code] [/quote] ok............dh try tp ade 6 error..........camni ke??????? #include<iostream> #include <string> using namespace std; using std::string; class Teacher { protected: char stafname[25],stafid[10]; public: void accept() { cout<<"\nEnter your name :"<<endl; cin>>stafname; cout<<"\nEnter staffid :"<<endl; cin>>stafid; } }; class Student:public Teacher { private: char Class[3],subject[12],name[25]; int *mark_bm1[100],*mark_bm2[100],*mark_math[100],*mark_science[100],*mark_bi[100]; int a,bil, total,*average[100];0 public: void Accept(); void display(); }; void Student::Accept() { cout<<"****************SELAMAT DATANG KE**************************"; cout<<"\nSISTEM PENGIRAAN PURATA PERMAKAHAN DAN GRED PELAJAR UPSR"; cout<<"\n*********************************************************"; cout<<"\nEnter Class :"; cin>>Class; cout<<"\nEnter number of student :"; cin>>bil; mark_bm1 = (int)malloc(sizeof(int)*bil); mark_bm2 = (int)malloc(sizeof(int)*bil); mark_math = (int)malloc(sizeof(int)*bil); mark_science = (int)malloc(sizeof(int)*bil); mark_bi = (int)malloc(sizeof(int)*bil); for(a=0;a<bil;a++) { cout<<"\nEnter student name :"; cin>>name; cout<<"\nEnter mark for Bahasa Melayu 1 :"; cin>>*mark_bm1[a]; cout<<"\nEnter mark for Bahasa Melayu 2 :"; cin>>*mark_bm2[a]; cout<<"\nEnter mark for Mathematics :"; cin>>*mark_math[a]; cout<<"\nEnter mark for Science:"; cin>>*mark_science[a]; cout<<"\nEnter mark for English:"; cin>>*mark_bi[a]; total=*mark_bm1[a]+*mark_bm2[a]+*mark_math[a]+*mark_science[a]+*mark_bi[a]; *average[a]=total/5; } } void Student::display() { cout<<"\n=========================================="; cout<<"\nClass :"<<Class; cout<<"\t\tBil. Student :"<<bil; for(a=0;a<bil;a++) { if(*mark_bm1[a]>=80) { cout<<"\nName :"<<name; cout<<"\n\nBahasa Melayu 1 :"<<*mark_bm1[a]; cout<<"\tGred A"; } else if(*mark_bm1[a]>59&&*mark_bm1[a]<80) { cout<<"\nName :"<<name; cout<<"\n\nBahasa Melayu 1 :"<<*mark_bm1[a]; cout<<"\tGred B"; } else if(*mark_bm1[a]>39&&*mark_bm1[a]<60) { cout<<"\nName :"<<name; cout<<"\n\nBahasa Melayu 1 :"<<*mark_bm1[a]; cout<<"\tGred C"; } else { cout<<"\nName :"<<name; cout<<"\n\nBahasa Melayu 1 :"<<*mark_bm1[a]; cout<<"\tGred D"; } if(*mark_bm2[a]>=80) { cout<<"\n\nBahasa Melayu 2 :"<<*mark_bm2[a]; cout<<"\tGred A"; } else if(*mark_bm2[a]>59&&*mark_bm2[a]<80) { cout<<"\n\nBahasa Melayu 2 :"<<*mark_bm2[a]; cout<<"\tGred B"; } else if(*mark_bm2[a]>39&&*mark_bm2[a]<60) { cout<<"\n\nBahasa Melayu 2 :"<<*mark_bm2[a]; cout<<"\tGred C"; } else { cout<<"\n\nBahasa Melayu 2 :"<<*mark_bm2[a]; cout<<"\tGred D"; } if(*mark_math[a]>=80) { cout<<"\n\nMathematics :"<<*mark_math[a]; cout<<"\t\tGred A"; } else if(*mark_math[a]>59&&*mark_math[a]<80) { cout<<"\n\nMathematics :"<<*mark_math[a]; cout<<"\t\tGred B"; } else if(*mark_math[a]>39&&*mark_math[a]<60) { cout<<"\n\nMathematics :"<<*mark_math[a]; cout<<"\t\tGred C"; } else { cout<<"\n\nMathematics :"<<*mark_math[a]; cout<<"\t\tGred D"; } if(*mark_science[a]>=80) { cout<<"\n\nScience :"<<*mark_science[a]; cout<<"\t\tGred A"; } else if(*mark_science[a]>59&&*mark_science[a]<80) { cout<<"\n\nScience :"<<*mark_science[a]; cout<<"\t\tGred B"; } else if(*mark_science[a]>39&&*mark_science[a]<60) { cout<<"\n\nScience :"<<*mark_science[a]; cout<<"\t\tGred C"; } else { cout<<"\n\nScience :"<<*mark_science[a]; cout<<"\t\tGred D"; } if(*mark_bi[a]>79) { cout<<"\n\nBahasa Inggeris :"<<*mark_bi[a]; cout<<"\tGred A"; } else if(*mark_bi[a]>59&&*mark_bi[a]<80) { cout<<"\n\nBahasa Inggeris :"<<*mark_bi[a]; cout<<"\tGred B"; } else if(*mark_bi[a]>39&&*mark_bi[a]<59) { cout<<"\n\nBahasa Inggeris :"<<*mark_bi[a]; cout<<"\tGred C"; } else { cout<<"\n\nBahasa Inggeris :"<<*mark_bi[a]; cout<<"\tGred D"; } cout<<"\n\nAverage :"<<*average[a]; } cout<<"\n=========================================="; } void main() { Student obj1; obj1.accept(); obj1.Accept(); obj1.display(); } Quote Share this post Link to post Share on other sites
syahmixp 44 Report post Posted April 4, 2011 Hee, bukan macam tu... Kod yang lain semuanya masih sama. Yang perlu diubah cuma semasa isytihar sahaja: [code]int a,bil,mark_bm1[ ],mark_bm2[ ],mark_math[ ],mark_science[ ],mark_bi[ ],total_bm1,total_bm2,total_math,total_science,total_bi;[/code]menjadi [code]int a,bil, total_bm1,total_bm2,total_math,total_science,total_bi; // ini adalah integer int *mark_bm1, *mark_bm2, *mark_math, *mark_science, *mark_bi; // ini adalah integer pointer yang AKAN mengandungi alamat nilai sebenar // kod dipisahkan supaya mudah baca[/code] Kemudian sediakan satu ruang pada memori untuk penunjuk tatasusunan tadi (yang ini betul) [code]mark_bm1 = (int)malloc(sizeof(int)*bil); mark_bm2 = (int)malloc(sizeof(int)*bil); mark_math = (int)malloc(sizeof(int)*bil); mark_science = (int)malloc(sizeof(int)*bil); mark_bi = (int)malloc(sizeof(int)*bil);[/code] Kemudian yang lain2 semuanya masih sama seperti kod asal, contohnya: [code]cout<<"\nEnter mark for Bahasa Melayu 1 :"; cin>>mark_bm1[a];[/code] Kemudian apabila tamat metod, bebaskan memori yang digunakan tadi supaya bahagian memori tersebut dapat digunakan semula dan mencegah pembaziran memori: [code]free(mark_bm1); free(mark_bm2); // seterusnya[/code] letak dihujung metod Dan lagi satu, saya tak pernah compile pun aturcara anda. Jadi minta maaf ye kalau masih ada yang silap. hehe Dan lagi satu, kod-kod aturcara elok diletak dalam code snippet supaya mudah dibaca [code][code]Kod disini[/code ][/code] Quote Share this post Link to post Share on other sites
otai_g 3 Report post Posted May 9, 2012 polymorphism == pewarisan. so mesti ada base class n derive class. base class + derive class = polymorphism. p/s: ada ms nanti aku alter code ko nih.. Quote Share this post Link to post Share on other sites