Author Topic: constructors  (Read 7044 times)

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
constructors
« on: February 15, 2006, 02:41:04 AM »
Sample code discussed in class

Code: [Select]

#include <iostream>
#include <cstring>

using namespace std;

class Beverage
{
      public:
             string brand;
             string flavor;
             double volume;
             double price;
             void sell();
             Beverage mix(Beverage otherdrink);
             double refill();
             void display();
             Beverage();
             Beverage(string pbrand, string pflavor,
                             double pvolume, double pprice);
             
      private:
              string secretIngr;
};

int main()
{
    Beverage juice;
    juice.brand = "del monte";
    juice.flavor = "orange";
    juice.volume = 375;
    juice.price = 22.50;
    juice.display();
 
    Beverage lambanog("lambanog", "salty", 400, 25.50);
    lambanog.display();
 
    Beverage tuba("tuba", "sour", 500, 15.75);
    tuba.display();
   
    cout << "mixing two drinks ... " << endl;
    Beverage urine = tuba.mix(lambanog);
    urine.display();
   
    system("PAUSE");
    return 0;
}

void Beverage::sell()
{
      // no implementation yet
}

double Beverage::refill()
{
      // no implementation yet
}

void Beverage::display()
{
     cout << "Brand  : " << brand << endl;
     cout << "Flavor : " << flavor << endl;
     cout << "Volume : " << volume << endl;
     cout << "Price  : " << price << endl << endl;
}

Beverage::Beverage()
{
     brand = "not assigned";
     flavor = "unknown";
     volume = 0;
     price = 0;
}

Beverage::Beverage(string pbrand, string pflavor,
                             double pvolume, double pprice)
{
     brand = pbrand;
     flavor = pflavor;
     volume = pvolume;
     price = pprice;                            
}

Beverage Beverage::mix(Beverage otherdrink)
{
     Beverage tmp;
     char newbrand[100] = "";
     strcat(newbrand, brand.c_str() );
     strcat(newbrand, "-");
     strcat(newbrand, otherdrink.brand.c_str() );
     tmp.brand = newbrand;
     
     char newflavor[100] = "";
     strcat(newflavor, flavor.c_str() );
     strcat(newflavor, "-");
     strcat(newflavor, otherdrink.flavor.c_str() );
     tmp.flavor = newflavor;
     
     tmp.volume = volume + otherdrink.volume;
     tmp.price = price + otherdrink.price;
     
     return tmp;
}

Analyze. Design. Develop. Debug. Deploy. Maintain.

quidato_aj

  • User
  • *
  • Posts: 1
    • http://www.yahoo.com
more about struc.....
« Reply #1 on: February 16, 2006, 04:00:30 AM »
sir, can you please tell me more about struct, and cdaccount functions.
when can  the dot(.) be used.? thank you!
iict rocks!

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
struct vs class
« Reply #2 on: February 17, 2006, 08:40:49 AM »
Think of struct as a primitive, less endowed version of class.  It has some of the features of a class, namely, you can create instance objects out of it and it can have instance variables.  However struct lacks the features that make the class better.  Classes can have private and public instance variables, and most importantly classes have member functions (methods).  Classes let you do all the cool things in OOP, encapsulation, inheritance, polymorphism.

It's true, everything a struct can do, a class can do too and even better.  So you should use struct only when you dont need the other features of a class.  Use a struct when you want objects that require less memory and dont need any member functions, dont need inheritance or polymorphism, etc.

There isn't such a thing as CDAccount functions. CDAccount is used as a sample both in the form of a struct and class.  That way, you could get the idea that a lot of new features are added when you create a class.

I sure hope you invest more time reading your textbook.  Our discussion here helps ease up the learning curve but it is no substitute to a thorough and reflective reading on the topic.
Analyze. Design. Develop. Debug. Deploy. Maintain.

Yap Michael

  • User
  • *
  • Posts: 8
..in the class Month
« Reply #3 on: February 18, 2006, 08:50:08 AM »
I'd like to join! Sir, i want to ask if the input function, in the problem regarding the class Month, will let the user enter the data? Or it is already inside the program?

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
source of test data
« Reply #4 on: February 18, 2006, 11:20:51 AM »
The problem is really about testing your skills in building a class.  It does not matter where your test data comes from when you use your class definition in a test program.  

I prefer to see several statements that make use of the class and these should include combination of test data both coming from input values within the program and others that are to be inputted by the user.
Analyze. Design. Develop. Debug. Deploy. Maintain.