Posts

Showing posts from October, 2015

Matrix Class C++ Example

Define matrix class with dynamic memory allocation for 2-D matrix. Include constructors, destructor, overload + (m1+m2, m1+3 & 4+m2), -, << and >> operators. Also overload new and delete operators by using malloc & free functions internally #include<iostream> using namespace std; #include<cstdlib> #include<new> class matrix { int **array; int row; int column; public: //default constructor matrix() { array=NULL; row=0; column=0; } //single parameter constructor(square matrix) matrix(int length) { int i,j; array=(int **)new int[length]; row=length; column=length; for(i=0;i<length;i++) array[i]=new int[length]; for(i=0;i<length;i++) for(j=0;j<length;j++) array[i][j]=0; } //double parameterized constructor(rectangular matrix) matrix(int r,int c) { int i,j; row=r; column=c; array=(int **)new int[r]; for(i=0;

Shallow Copy and Deep Copy Example

Define string class with dynamic memory allocation for string. Define default constructor, parameterized constructors, copy constructor, destructor, Overload +, [], =, <<, >> operators. Observe the behavior of shallow copying and deep copying. #include<iostream> using namespace std; #include<string.h> class cstring { char *sptr; int length; public: cstring() { sptr=NULL; length=0; } cstring(const char *s) { sptr=new char[strlen(s)+1]; strcpy(sptr,s); length=strlen(s); } cstring(const cstring &s) { sptr=new char[s.length+1]; strcpy(sptr,s.sptr); length=s.length; } void print_cstring() { cout<<sptr<<endl; } void scan_cstring() { sptr=new char[10]; cin>>sptr; } ~cstring() { if(sptr!=NULL) delete []sptr; } cstring operator=(cstring c) { delete []sptr; sptr=new char[strlen(c.sptr)+1]; strcpy(sptr,c.sptr); length=c.length; retu

Complex class C++ Example

Define a complex class, define default constructor, parameterized constructors, copy constructor, destructor. Overload +, -, unary -, ++ (prefix, postfix), =, comma (,), ->, << and >> operators. #include<iostream> using namespace std; #include<math.h> //defining the complex class class complex { //private members float real; float img; //publc members public: //constructors are used to initialize the objects //default constructor complex() { real=0; img=0; } //parameterized constructor complex(float a,float b) { real=a; img=b; } //copy constructor complex(const complex &c) { real=c.real; img=c.img; } //destructors is used to deallocate the data stored ~complex() { cout<<"destroyed"; } void get_complex() { cout<<"\n enter the real value"; cin>>real; cout<<"\n enter the imaginary value:"; cin>&g

Polar Class Example C++

Define polar class where each object represents point in polar coordinates (angle and radius). Include static data member which keeps track the number of active objects of the class #include<iostream> #include<math.h> using namespace std; class polar { float real; float img; public: static int count; void get_polar() { count++; cin>>real>>img; } void print_polar() { cout<<real<<img; } polar() { count++; real=img=0; } polar(const polar &p1) { count++; img=p1.img; real=p1.real; } ~polar() { count--; cout<<"DESTRUCTOR\n"; } }; int polar::count; int main() { polar c1,c2,c3,c4; c1.get_polar(); c2.get_polar(); c3.get_polar(); c4.get_polar(); cout<<polar::count; c1.~polar(); c2.~polar(); c3.~polar(); c4.~polar(); return 0; }