333Test2

  1. Address-of operator
    &
  2. myvar = 25;
    foo = &myvar;
    bar = myvar;
    Interpret foo = &myvar;
    • This would assign the address of variable myvar to foo;
    • &myvar is the address of myvar
  3. Dereference operator
    *
  4. baz = *foo;
    • baz is the value stored in memory location foo.
    • "baz equal to value pointed to by foo"
  5. & is the address-of operator, and can be read simply as
    "address of"
  6. * is the dereference operator, and can be read as
    "value pointed to by"
  7. baz = foo;
    versus
    baz = *foo;
    • baz = foo; // baz equal to foo (1776)
    • baz = *foo; // baz equal to value pointed to by foo (25)
  8. foo = &myvar; implies
    *foo == myvar
  9. The stack:
    All variables declared inside the function will take up memory from the stack.
  10. The heap:
    This is unused memory of the program and can be used to allocate the memory dynamically when program runs.
  11. You can allocate memory at run time within the heap for the variable of a given type using
    a special operator in C++ which returns the address of the space allocated. This operator is called new operator.
  12. delete operator
    If you are not in need of dynamically allocated memory anymore, you can use delete operator, which de-allocates memory previously allocated by new operator.
  13. generic syntax to allocate memory dynamically for any data-type.
    new data-type;
  14. dynamically allocate space for five elements of type int and return a pointer to the first element, which is assigned to foo.
    • int * foo;
    • foo = new int [5];
  15. ptr or ptr[] is no longer needed
    • delete ptr;
    • delete[] ptr;
  16. Classes are defined using
    • class class_name
    • {
    • access_specifier_1:
    •    member1;
    • access_specifier_2:
    •   member2;
    • ...
    • } object_names;
  17. private
    private members of a class are accessible only from within other members of the same class (or from their "friends").
  18. protected
    protected members are accessible from other members of the same class (or from their "friends"), but also from members of their derived classes.
  19. public
    Finally, public members are accessible from anywhere where the object is visible.
  20. class Pump {
    private: int amtGasTank; int tankCapacity;
    A function called showPump()that displays its members to a file or screen.
    • void showPump(ostream & /*out*/) const;
    • void showPump(ostream output) const
    • {output<<amtGasTank<<tankCapacity;}
  21. class Pump {
    private: int amtGasTank; int tankCapacity;
    Change the access specifier for the base class to allow access to its data members from anyderived class but not a client of the class.
    • class Pump
    • {
    • protected:
    •      int amtGasTank
    •     int tankCapacity;
  22. class Pump {private: int amtGasTank; int tankCapacity;
    Write the default and non-default constructors
    • Pump();
    • Pump(int /*amtGasTank*/, int/*capac*/);
    • Pump::Pump()
    • { ask or assign }
    • Pump::Pump(int newAmtGasTank, int newTankCapacity)
    • {    tankCapacity = (newTankCapacity >  &&
    •     newTankCapacity < 100)?
    •     newTankCapacity : 50;
    • amtGasTank = (newAmtGasTank > 0 &&
    •     newAmtGasTank < tankCapacity)?
    •     newAmtGasTank : 0;
    • }
  23. ->
    • The arrow operator (->) is a dereference operator that is used exclusively with pointers to objects that have members. This
    • operator serves to access the member of an object directly from its address.
  24. pmovie->title
    is equivalent to
    (*pmovie).title
  25. how do you postpone binding
    write virtual infront of the prototype in base class
  26. int *ptr1, *ptr2;
    ptr1= new int;
    *ptr1 = 88;
    ptr2=ptr1;
    cout << "*ptr1 = " << * ptr1 << endl;
    cout << "*ptr2 = " << * ptr2 << endl;
    *ptr2 = 42;
    cout << "*ptr1 = " << *ptr1 << endl;
    cout << "*ptr2 = " << *ptr2 <<endl;
    ptr1 = new int;
    *ptr1 = 53;
    cout << "*ptr1 = " << *ptr1 << endl;
    cout << "*ptr2 = " << *ptr2 << endl;
    • *ptr1=88
    • *ptr2=88
    • *ptr1=42
    • *ptr2=42
    • *ptr1=53
    • *ptr2=42
  27. int *ptr1, *ptr2;
    ptr1= new int;
    *ptr1 = 88;
    ptr2=ptr1;
    cout << "*ptr1 = " << * ptr1 << endl;
    How do remove all that was allocated dynamically?
    delete ptr1, ptr2;
  28. class Cat: public Pet
    {public:
    Cat(string /*name*/, string/*breed*/,
         int/*age*/,char /*sex*/);
    void show() const;
    virtual ~Cat();
    private:
        string *breed;
        int age;
        char sex;
    };
    Declare an object of type Cat on the stack called kitty, passing it hard coded values.
    also on the heap
    Cat kitty(“Kitty”, “Siamese”, 4,’M’);
  29. class Cat: public Pet
    {public:
    Cat(string /*name*/, string/*breed*/,
         int/*age*/,char /*sex*/);
    void show() const;
    virtual ~Cat();
    private:
        string *breed;
        int age;
        char sex;
    };
    Declare an object of type Cat on the stack called kitty, passing it hard coded values.
  30. class Cat: public Pet
    {
    public:
        Cat(string /*name*/, string/*breed*/,
         int/*age*/,char /*sex*/);
        void show() const;
        virtual ~Cat();
    private:
        string *breed;
        int age;
        char sex;
    };
    Declare an object dynamically of type Cat, passing it hard coded values. Declare any variables that you require. Ensure memory was allocated.
    • Cat *objPtr=new Cat(“Kitty”, “Siamese”,
    •      4,’M’);
    • assert(objPtr!=0);
  31. Cat *objPtr = new Cat(“Kitty”, “Siamese”,
        4,’M’);
    assert(objPtr!=0);
    c) Print the contents of the object created in b) above, by calling the member function show().
    objPtr->show();
  32. void showPet(const Pet* object)//client function
    {
        object->show();
    }
    Cat *objPtr=new Cat(“Kitty”, “Siamese”,
        4,’M’);
    Call the showPet function to print the contents of kitty.
    showPet(objPtr);
  33. Add a friend function to the GasPump class that will display the dollar value of the amount of gasoline in the tank.
    prototype?
    function definition?
    call?
    • class GasPump : public Pump
    • {public:
    •    friend void showTankValue(const GasPump *);
    • ...};
    • void showTankValue(const GasPump * obj)
    • {
    •    cout<< (obj->gasPrice*obj->amtGas);
    • }
    • showTankValue(ptr2GasPumpObject);
Author
slc53
ID
325599
Card Set
333Test2
Description
333Test2
Updated