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

كود بالكلاس

بدأه سرمد الليل في 31 مارس 2010 · 6 رد · 1,167 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

هذا السوال:

Create product class. It contains name, price, discount percentage and price after discount as data members.

- Define a default constructor to initialize all data members.

- Define a member function that uses price and discount percentage to calculate and set the price after discount. Note that a member function can call another member function.

- Create a non member function displayInfo that receives the product from the main. Display all the object information and also print the price after discount.

- In the main let the user enters the product name, discount percentage and price. Next, set the product object with this information. Finally calculate the price after discount and call diaplyinfo().

محاولتى فى الحل:

#include<iostream>

using namespace std;

class price{

int pric;

int discount percentage;

int price after discount;

public:

price( int price, int discount percentage ,int price after discount ){

price-= discount percentage;

cout<<"Enter product price\n ";

cin>>price;

cout<<"Enter the discount percentage\n";

cin>>discount percentage;

cout"Displaying product information\n";

cout<<"Price"<<price;

cout<<"Discount Percentage "<<discount percentage;

cout<<"Price After discount "<<price after discount;

}

};

int main()

(int ID;

price p;

cout<<"Enter product ID\n";

cin>>ID;

p.price;

return 0;

)

تم تعديل هذه المشاركة بواسطة سرمد الليل في 31 مارس 2010 في 20:05

#2

تفضل اخي سرمد وهذا التعديل مع الشرح .

#include<iostream>

using namespace std;


/*

Create product class. It contains name, price, discount percentage and price after discount as data members. 

*/

class product
{

	float price,disper,priceAD; /* disper=discount precentage, priceAD=price after discount */
	char name[10];
public:
	//- Define a default constructor to initialize all data members. 
	product()
	{
	 name[0]='\0'; //make the first element blank that will make array empty
	 price=0.0;
	 disper=0.0;
	 priceAD=0.0;
	}
	//Define a member function that uses price and discount percentage to calculate and set the price after discount
float AfterDiscount(float p,float d)
{
  return(p-((p*d)/100));
}

	void insert()
		{	

		cout<<"Enter the product Name: " ;
		cin>>name;
		cout<<"Enter product price: ";
		cin>>price;
		cout<<"Enter the discount percentage % : ";
		cin>>disper;
  //Call the function to calculate the price after discount.
		priceAD=AfterDiscount(price,disper);
	}
	//- Create a non member function displayInfo that receives the product from the main. 
 friend void displayInfo(product);
};

//- Create a non member function displayInfo that receives the product from the main. Display all the object information and also print the price after discount.
// Non-Member function

void displayInfo(product p)
{
  cout<<"\n====== The Item in in the Product ======\n\n";  

  cout<<"Product Name         :"<<p.name<<endl;
  cout<<"Product Price        :"<<p.price<<endl;
  cout<<"Product Discount     :"<<p.disper<<"%"<<endl;
  cout<<"Price after discount :"<<p.priceAD<<endl;

}


int main()
{
	product p;
	//insert the Data to object members using member function
	p.insert();
	//send Object to non-member function to display
	displayInfo(p);
	system("pause");
return 0;
}

تحياتي

1

OMANI FOR EVER

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

post-12787-12780191801974.jpg

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

#3

السلام عليكم

انا حاب استفسر عن الكود ليش حطيت هذي الداله لاني اناا عندي نفس هذا السؤال مطلوب حله بس مافهمت ليش استخدمت هذي الداله لان هذي الداله اول مرع اشوفهاا

friend void displayInfo(product);

عضوية مشطوبة - spam

#4
حلمي ويستحق العناء كتب:

السلام عليكم

انا حاب استفسر عن الكود ليش حطيت هذي الداله لاني اناا عندي نفس هذا السؤال مطلوب حله بس مافهمت ليش استخدمت هذي الداله لان هذي الداله اول مرع اشوفهاا

friend void displayInfo(product);

friend function تم وضعها بسبب هذه الجملة من السؤال

Create a non member function displayInfo that receives the product from the main. Display all the object information and also print the price after discount

حيث أن الجملة تقول انشئ فنكشن لاتكون عضوا في الكلاس ومن مهامها عرض كل البيانات الموجودة بالكلاس . ويجب ان تستلم الكائن ( الكلاس بكل عناصره) من الدالة الرئيسية Main

تحياتي

1

OMANI FOR EVER

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

post-12787-12780191801974.jpg

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

#5

طيب اخوي انا لسه مااخذت هذي الداله اللي اسمها friend فكيف استخدمهاا وانا ماعرفها

طيب انا جربت احله بالقت ويطلعلي ارور ماني عارفه ليه ممكن تشوفلي كودي هذا هو

#include<iostream>

using namespace std;

class product

{

int ID;

int price;

int disper;

int priceAD;

string name;

public:

product()

{

name=" ";

price=0;

disper=0;

priceAD=0;

ID =0;

}

float AfterDiscount(int p,int d)

{

return(p-((p*d)/100));

}

void insert()

{

cout<<"Enter the product ID:\n " ;

cin>>ID;

cout<<"Enter product price:\n ";

cin>>price;

cout<<"Enter the discount percentage : \n";

cin>>disper;

priceAD=AfterDiscount(price,disper);

}

};

void displayInfo()

{

cout<<"Displaying product information \n";

cout<<"Product Price "<< getprice<<endl;

cout<<"Product Discount "<<getdisper<<endl;

cout<<"Price after discount "<<getpriceAD<<endl;

}

int main()

{

product p;

p.insert();

displayInfo();

return 0;

}

عضوية مشطوبة - spam

#6

السلام عليكم

شكرا لك لمجهودك ومساعدتك جعلها الله في ميزان حسناتك

لكن عندي سؤال بخصوص

لماذا وضعت في البدايه وعطيتها القيمه صفر

name[0]='\0';

price=0.0;

disper=0.0;

priceAD=0.0;

وبعد سؤالي نفس الاخت حملي ويستحق العناء

سبحانك اللهم وبحمدك لا اله الا انت استغفرك واتوب اليك ‘‘

#7
حلمي ويستحق العناء كتب:

طيب اخوي انا لسه مااخذت هذي الداله اللي اسمها friend فكيف استخدمهاا وانا ماعرفها

طيب انا جربت احله بالقت ويطلعلي ارور ماني عارفه ليه ممكن تشوفلي كودي هذا هو

لانكم لم تدرسوا friend فلن نتمكن من استخدامها .

في هذه الحالة نستخدم دالة للطباعة داخل الكلاس . ونقوم بإستدعائها من الفانكشن الخارجية. بعد تمرير الكائن الى الفانكشن displayInfo . لتقوم بدورها باستدعاء الدالة المخصصة لطباعة النتائج . والسبب وجود البيانات Private في الكلاس . فلايمكن الوصول الىها إلا عن طريق فانكشن يكون تابع لنفس الكلاس أم مايسمي function member

وهذا مثالك بعد التعديل .

#include<iostream>
#include<string>

using namespace std;

class product
{
int ID;
float price;
int disper;
float priceAD;
string name;
public:
product()
{
name=" ";
price=0;
disper=0;
priceAD=0;
ID =0;
}
float AfterDiscount(float p,int d)
{
return(p-((p*d)/100));
}

void insert()
{

cout<<"Enter the product ID:\n " ;
cin>>ID;
cout<<"Enter product Name:\n ";
cin>>name;
cout<<"Enter product price:\n ";
cin>>price;
cout<<"Enter the discount percentage : \n";
cin>>disper;
priceAD=AfterDiscount(price,disper);
}

void printData()
{
cout<<"Product ID          :"<<ID<<endl;
cout<<"Product Name        :"<<name<<endl;
cout<<"Product Price       :"<< price<<endl;
cout<<"Product Discount    :"<<disper<<endl;
cout<<"Price after discount:"<<priceAD<<endl;
}


};

void displayInfo(product p)
{
cout<<"Displaying product information \n";
p.printData();

}


int main()
{
product p;
p.insert();
displayInfo(p);
system("pause");
return 0 ;
}

تحياتي

miss fno كتب:

السلام عليكم

شكرا لك لمجهودك ومساعدتك جعلها الله في ميزان حسناتك

لكن عندي سؤال بخصوص

لماذا وضعت في البدايه وعطيتها القيمه صفر

name[0]='\0';

price=0.0;

disper=0.0;

priceAD=0.0;

وبعد سؤالي نفس الاخت حملي ويستحق العناء

هنا السؤال بأكملة

هذا السوال:

Create product class. It contains name, price, discount percentage and price after discount as data members.

- Define a default constructor to initialize all data members.

- Define a member function that uses price and discount percentage to calculate and set the price after discount. Note that a member function can call another member function.

- Create a non member function displayInfo that receives the product from the main. Display all the object information and also print the price after discount.

- In the main let the user enters the product name, discount percentage and price. Next, set the product object with this information. Finally calculate the price after discount and call diaplyinfo().

لاحظي الجزء الملون بالاحمر . ستجدين أنه في السؤال طلب انشاء constructor يقوم باعادة كتابة القيم .

وسؤالك يخص هذا الجزء بالتحديد.

تحياتي

1

OMANI FOR EVER

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

post-12787-12780191801974.jpg

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

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