You are on page 1of 10

Dynamic Memory Allocation

Variables are created & destroyed while a program is running.

Ex:
int *p, *q, x=5;
p=&x;
q=new int; //allocates memory for a new integer type variable (nameless)
*q=8; // dereferencing
cout<<*q;
cin>>*q;
sum += *q;
. . .
delete q; //memory released for future use.
CMPE225 Object-Oriented Programming 1
Dynamic Memory Allocation cont.
Dynamically created array:

int *arr;
arr=new int[100];
arr[2]=7;
for(int i=0; i<100; i++)
cin>>arr[i];
. . .
delete []arr;

Always delete variables created by new, otherwise they will become


garbage.
CMPE225 Object-Oriented Programming 2
Unary Scope Resolution Operator
• To access a global name when a local variable with the same name exists.
Ex:
void f();
int x=5;
main()
{ ...
}

void f()
{ double x=2.2;
cout<<x<<endl; //local
cout<<::x; //global
::x=8; //global //:: is unary scope resolution operator
}
CMPE225 Object-Oriented Programming 3
CLASS
• The class construct allows you to define a new type of data according to the needs of the
problem to be solved, & to define operators / functions to act upon these types.
• This way the attributes (data) & behaviours (functions) are encapsulated.

Syntax:
class Classname class declaration (no memory space is reserved)
Member { public : //members can be accessed from anywhere, optional
access ...
specifiers
...
private : //members can only be accessed from the members of the class,
... // default, optional
...
};

CMPE225 Object-Oriented Programming 4


Ex:
#include <iostream>
using namespace std;

class One class declaration


{ public:
int x; data member
void print() member function
{ cout<<x; }
};

main()
{ One obj; // create obj to be an object / instance of class One
obj.x=5; // access x member of obj
obj.print(); // call member function print()
}

CMPE225 Object-Oriented Programming 5


Declaring Objects:
One obj; // object / instance of class One
One arr[10]; // array of 10 One type object
One *p = new One; // pointer to a nameless object
One &r = obj; // reference to object obj

Accessing Members:
obj.x = 0;
arr[2].x = 0;
p->x = 0;OR (*p).x = 0;
r.x = 11;

CMPE225 Object-Oriented Programming 6


Ex:
class Two
{ public: a separate file,
int i; save it under the name of "two.h"
void print() { x=2.5;
cout<<i<<endl; }
private:
float x;
};

#include <iostream>
#include "two.h"
main()
{ Two t; another file
t.i=5;
t.print();
t.x=5.5; X ERROR!
}
CMPE225 Object-Oriented Programming 7
• inline member functions are completely defined within the context of the class definition. Define
larger functions outside the class with :: Binary Resolution Operator.

Ex:
class Three
{ public:
int i;
void print(); //function prototype
};
void Three :: print()
{ cout<<"Hello"<<endl; func. definition
cout<<"i= "<<i<<endl;
}

main()
{ Three a;
a.i=8;
a.print();
}
CMPE225 Object-Oriented Programming 8
Ex:
class Rectangle
{ private:
int width, length;
public:
void setWidth(int);
void setLength(int);
int area();
};

void Rectangle::setWidth(int w)
{ width=w; }

void Rectangle::setLength(int l)
{ length=l; }

int Rectangle::area()
{ return width*length; }

main()
{ Rectangle r;
r.setWidth(5);
r.setLength(3);
cout<<r.area();
}
CMPE225 Object-Oriented Programming 9
Ex: Write a program (wap) to process a bank account.
main()
{ Account mine;
Ex: char action;
#include <iostream> int amount;
cout<<"Enter initial balance: ";
using namespace std;
cin>>amount;
mine.initialize(amount);
class Account do{
{ int balance; // private cout<<"Enter B-Balance, W-Withdraw, D-Deposit, Q-Quit: ";
cin>>action;
public: if(action == 'B')
void initialize (int); cout<<"Balance: "<<mine.check()<<endl;
int check() {return balance; } else if(action == 'W')
{ cout<<"Amount to withdraw: ";
cin>>amount;
void withdraw (int); mine.withdraw(amount);
void deposit (int money) {balance+=money; } }
else if(action == 'D')
}; { cout<<"Amount to deposit: ";
cin>>amount;
mine.deposit(amount);
void Account::initialize (int x) }
{ balance=x; } } while (action != 'Q');
}

void Account::withdraw (int x)


{ if(balance < x)
cout<<"not enough money"<<endl;
else
balance-=x; } CMPE225 Object-Oriented Programming 10

You might also like