Posts

Showing posts with the label example

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>...

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; }

Point Class Example C++

Define a point class, where each object represents point in Cartesian coordinates (x, y). Define objects of this class and calculate the distance between the 2 points through a friend function #include<iostream> #include<math.h> using namespace std; class point { float x; float y; public: point() { x=0; y=0; } point(int z) { x=y=z; } point(int a,int b) { x=a; y=b; } point(const point &c) { x=c.x; y=c.y; } void get_point() { cin>>x>>y; } void print_point() { cout<<x<<y; cout<<"\n"; } friend float dist_point(point p1,point p2); }; float dist_point(point p1,point p2) { float result; result=sqrt(((p2.x-p1.x)*(p2.x-p1.x))+((p2.y-p1.y)*(p2.y-p1.y))); return result; } int main() { point c,c1,c2,c3; float distance; c.print_point(); c1.print_point(); c2.print_point(); c3.print_point(); c.get_point(); c1...

StringCchCat Function example

StringcCchCat Function: Operation : Concatenates one striing to another string. Syntax :   HRESULT StringCchCat( _Inout_ LPTSTR  pszDest, _In_    size_t  cchDest, _In_    LPCTSTR pszSrc ); Arguments : pszDest: Destination buffer to which pszSrc will be concatenated. cchDest: cch stands for count of characters. This argument is mainly  provided so that we will not be writing after the end of  buffer pszSrc: Null terminated string which you want to concatenate. Header File : #include <strsafe.h> Return Value: Type: HRESULT S_OK If successful We will see the other return values also Sample Code1 : #include "stdafx.h" #include "stdlib.h" #include "winerror.h" #include "windows.h" #include "strsafe.h" int _tmain(...

_countof macro example

_countof() macro returns the number of elements present in the array. It is different from sizeof() , which returns the size of the array in bytes. Syntax : size_t _countof(array);  You should include the header file #include <stdlib.h> to use it. Example1: #include "stdafx.h" #include "stdlib.h" int _tmain(int argc, _TCHAR* argv[]) { TCHAR buffer[100]; printf("Size of buffer is %d and _countof buffer is %d\n", sizeof(buffer), _countof(buffer)); return 0; } O/P: Size of buffer is 200 and _countof buffer is 100. So from the above output you can see that ouput of sizeof and _countof is different, _countof returns the number of elements present in the array whereas sizeof returns the number of bytes occupied by the array. Don't try to pass an pointer as argument to _countof, pass only array. You will get a compile time error . For example look at the following code below. #include "stdafx.h" #include...