Professional Documents
Culture Documents
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;
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
...
};
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()
}
Accessing Members:
obj.x = 0;
arr[2].x = 0;
p->x = 0;OR (*p).x = 0;
r.x = 11;
#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');
}