Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Programming Practices
Class Definitions
class Box { public: double length; double breadth; double height; };
int main( ) { Box Box1; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box double volume = 0.0; // Store the volume of a box here // box 1 specification Box1.setheight(5.0); Box1.setlength( 6.0); Box1.setbreadth( 7.0); // box 2 specification Box2.setheight(10.0); Box2.setlength(12.0); Box2.setbreadth(13.0); // volume of box 1 volume = Box1.getheight() * Box1.getlength() * Box1.getbreadth(); cout << "Volume of Box1 : " << volume <<endl; // volume of box 2 volume = Box2.getheight() * Box2.getlength() * Box2.getbreadth(); cout << "Volume of Box2 : " << volume <<endl; return 0; }
Exercise
Create a Account Class with Attributes Account Name , Account No and Amount. Your Amount and Account Number Should be Private. Keep record of five Accounts. Take input Account Name, Account no and Amount from user. Print the values on screen of all Account Holders.
Constructor
A class can include a special function called constructor, which is automatically called whenever a new object of this class is created. This constructor function must have the same name as the class, and cannot have any return type; not even void. We are going to implement CRectangle including a constructor:
// example: class constructor #include <iostream> using namespace std; class CRectangle { int width, height; public: CRectangle (int,int); int area () {return (width*height);} }; CRectangle::CRectangle (int a, int b) { width = a; height = b; }
int main () { CRectangle rect (3,4); CRectangle rectb (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; return 0; }
Exercise
Modify the Account Class and Add Constructor in it with default NULL values.
Destructors
The use of destructors is especially suitable when an object assigns dynamic memory during its lifetime and at the moment of being destroyed we want to release the memory that the object was allocated.
// example on constructors and destructors #include <iostream> using namespace std; class CRectangle { int *width, *height; public: CRectangle (int,int); ~CRectangle (); int area () {return (*width * *height);} }; CRectangle::CRectangle (int a, int b) { width = new int; height = new int; *width = a; *height = b; } CRectangle::~CRectangle () { delete width; delete height; } int main () { CRectangle rect (3,4), rectb (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; return 0; }
Overloading Constructor
// overloading class constructors #include <iostream> using namespace std; class CRectangle { int width, height; public: CRectangle (); CRectangle (int,int); int area (void) {return (width*height);} }; CRectangle::CRectangle () { width = 5; height = 5; } CRectangle::CRectangle (int a, int b) { width = a; height = b; } int main () { CRectangle rect (3,4); CRectangle rectb; cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; return 0; }
What Is the difference Between CRectangle() and CRectangle(int a, int b); Debug the program and check the output of first two line in main().What is the Difference.
Final Exercise
Create a class Transaction with the attributes Account Name, Account Number, Amount. Your Program should have space for 10 users to create accounts. User should also have an option to delete the account. Give a user an interface to Check amount, deposit amount and withdrawal amount. Print message if user have no amount in account and he want to withdrawal of amount.
Checked By:
Date: