-
-
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
-
-
baz = *foo;
- baz is the value stored in memory location foo.
- "baz equal to value pointed to by foo"
-
& is the address-of operator, and can be read simply as
"address of"
-
* is the dereference operator, and can be read as
"value pointed to by"
-
baz = foo;
versus
baz = *foo;
- baz = foo; // baz equal to foo (1776)
- baz = *foo; // baz equal to value pointed to by foo (25)
-
foo = &myvar; implies
*foo == myvar
-
The stack:
All variables declared inside the function will take up memory from the stack.
-
The heap:
This is unused memory of the program and can be used to allocate the memory dynamically when program runs.
-
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.
-
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.
-
generic syntax to allocate memory dynamically for any data-type.
new data-type;
-
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];
-
ptr or ptr[] is no longer needed
-
Classes are defined using
- class class_name
- {
- access_specifier_1:
- member1;
- access_specifier_2:
- member2;
- ...
- } object_names;
-
private
private members of a class are accessible only from within other members of the same class (or from their "friends").
-
protected
protected members are accessible from other members of the same class (or from their "friends"), but also from members of their derived classes.
-
public
Finally, public members are accessible from anywhere where the object is visible.
-
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;}
-
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;
-
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;
- }
-
->
- 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.
-
pmovie->title
is equivalent to
(*pmovie).title
-
how do you postpone binding
write virtual infront of the prototype in base class
-
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
-
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;
-
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’);
-
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.
-
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);
-
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();
-
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);
-
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);
|
|