Computer Science, asked by happymall, 8 months ago

Create a class called Cereal that accepts three inputs: 2 strings and 1 integer, and assigns them to 3 instance variables in the constructor: name, brand, and fiber. When an instance of Cereal is printed, the user should see the following: “[name] cereal is produced by [brand] and has [fiber integer] grams of fiber in every serving!” To the variable name c1, assign an instance of Cereal whose name is "Corn Flakes", brand is "Kellogg's", and fiber is 2. To the variable name c2, assign an instance of Cereal whose name is "Honey Nut Cheerios", brand is "General Mills", and fiber is 3. Practice printing both!

Answers

Answered by mad210203
1

Program is given below.

Explanation:

#include <iostream>

using namespace std;

//Declaration of class

class Cereal

{

   char name[20], brand[20];

   int fiber,i;

   public:

//Constructor

   Cereal(char a[20], char b[20], int c)

   {   for(i=0;i<20;i++)

       {

         name[i]=a[i];

         brand[i]=b[i];

       }

       fiber=c;

   }

// This function is used to print the values.

   void Display()

   {

   cout<<name<<" cereal is produced by "<<brand<<" and has "<<fiber<<" grams of fiber in every serving!"<<endl;

   }

};

int main() {

  Cereal c1("Corn Flakes","Kellogg's",2);

   c1.Display();

   cout<<endl;

   Cereal c2("Honey Nut Cheerios","General Mills",3);

   c2.Display();

   return 0;

}

Refer the attached image for the output.

Attachments:
Answered by nandyamit27031999
0

Answer:

class Cereal():

   def __init__(self,name,brand,fiber):

       self.name=name

       self.brand=brand

       self.fiber=fiber        

   

   def __str__(self):

       return "{} cereal is produced by {} and has {} grams of fiber in every serving!".format(self.name, self.brand,self.fiber)

c1=Cereal("Corn Flakes","Kellogg's",2)

c2=Cereal("Honey Nut Cheerios","General Mills",3)

print(c1)

print(c2)

Explanation:

Similar questions