| Name |
Code |
Abstract Class Example
|
#include <iostream>
using namespace std;
// Base class
class Shape
{
public:
// pure virtual function providing interface framework.
virtual int getArea() = 0;
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
// Derived classes
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};
class Triangle: public Shape
{
public:
int getArea()
{
return (width * height)/2;
}
};
int main(void)
{
Rectangle Rect;
Triangle Tri;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total Rectangle area: " << Rect.getArea() << endl;
Tri.setWidth(5);
Tri.setHeight(7);
// Print the area of the object.
cout << "Total Triangle area: " << Tri.getArea() << endl;
return 0;
}
|
Accessing Characters In Strings
|
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string text;
cout << "Counts words. Enter a text and terminate with a period and return:\n";
getline( cin, text, '.'); // Reads a text up to the first '.'
int i, // Index
numberOfWhiteSpace = 0, // Number of white spaces
numberOfWords = 0; // Number of words
bool fSpace = true; // Flag for white space
for( i = 0; i < text.length(); ++i)
{
if( isspace( text[i]) ) // white space?
{
++numberOfWhiteSpace;
fSpace = true;
}
else if( fSpace) // At the beginning of a word?
{
++numberOfWords;
fSpace = false;
}
}
cout << "\nYour text contains (without periods)"
<< "\ncharacters: " << text.length()
<< "\nwords: " << numberOfWords
<< "\nwhite spaces: " << numberOfWhiteSpace
<< endl;
return 0;
}
|
Armstrong Number
|
# include <iostream.h>
# include <conio.h>
# include <math.h>
void main ()
{
clrscr();
int a,b=0,sum=0;
long int n;
cout<<"Enter the NO. : ";
cin>>n;
for(;n>0;)
//counts the digits
{
a=n%10;
n=n/10;
b++;
}
for(;n>0;)
{
a=n%10;
sum=sum+pow(a,b);
n=n/10;
}
if(sum==n)
{
cout<<"IT IS AN ARMSTRONG NUMBER...";
getch();
}
else
{
cout<<"IT IS NOT AN ARMSTRONG NUMBER...";
getch();
}
}
|
Accessing Data in a File
|
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
template <class T>
void print(T & c)
{
for( typename T::iterator i = c.begin(); i != c.end(); i++ )
{
std::cout << *i << endl;
}
}
int main()
{
vector <int> output_data( 10 );
generate( output_data.begin(), output_data.end(), rand );
transform( output_data.begin(), output_data.end(),output_data.begin(), bind2nd( modulus<int>(), 10 ) );
ofstream out( "data.txt" );
if( !out )
{
cout << "Couldn't open output file\n";
return 0;
}
copy( output_data.begin(), output_data.end(),ostream_iterator<int>( out, "\n" ) );
out.close();
ifstream in( "data.txt" );
if( !in )
{
cout << "Couldn't open input file\n";
return 0;
}
vector<int> input_data( (istream_iterator<int>( in )),istream_iterator<int>() );
in.close();
print( output_data );
print( input_data );
}
|
Accessing Static members without an object
|
#include <iostream>
using namespace std;
class Cat
{
public:
Cat(int age):itsAge(age){count++; }
virtual ~Cat() { count--; }
virtual int GetAge() { return itsAge; }
virtual void SetAge(int age) { itsAge = age; }
static int count;
private:
int itsAge;
};
int Cat::count = 0;
void TelepathicFunction();
int main()
{
const int MaxCats = 5; int i;
Cat *CatHouse[MaxCats];
for (i = 0; i< MaxCats; i++)
{
CatHouse[i] = new Cat(i);
TelepathicFunction();
}
for ( i = 0; i< MaxCats; i++)
{
delete CatHouse[i];
TelepathicFunction();
}
return 0;
}
void TelepathicFunction()
{
cout << "There are ";
cout << Cat::count << " cats alive!\n";
}
|
A Generic bubble sort
|
#include <iostream>
using namespace std;
template <class X>
void bubble(X *data, int size)
{
register int a, b;
X t;
for(a=1; a < size; a++)
for(b=size-1; b >= a; b--)
if(data[b-1] > data[b])
{
t = data[b-1];
data[b-1] = data[b];
data[b] = t;
}
}
int main()
{
int i[] = {3, 2, 5, 6, 1, 8, 9, 3, 6, 9};
double d[] = {1.2, 5.5, 2.2, 3.3};
int j;
bubble(i, 10); // sort ints
bubble(d, 4); // sort doubles
for(j=0; j<10; j++)
cout << i[j] << ' ';
cout << endl;
for(j=0; j<4; j++)
cout << d[j] << ' ';
cout << endl;
return 0;
}
|
Area Overloded
|
#include<iostream.h>
#include<conio.h>
#define phi 3.14
int area(int,int);
float area(int);
void main()
{
int a,b,c,cho;
clrscr();
cout<<"\t What do you want to do?\n";
cout<<"1. area of rectangle"<<endl;
cout<<"2. area of circle"<<endl;
cout<<"Choice:";
cin>>cho;
switch(cho)
{
case 1:
cout<<"Enter lengt and breath (with white space):";
cin>>a>>b;
cout<<"Area of RECTANGLE:"<<area(a,b);
break;
case 2:
cout<<"Enter radius:";
cin>>c;
cout<<"Area of CIRCLE:"<<area(c);
break;
}
getch();
}
int area(int x,int y)
{
return (x*y);
}
float area(int s)
{
return (phi*s*s);
}
|
A Simple Example of Inheritance
|
#include <iostream>
using namespace std;
class BaseClass
{
int i;
public:
void setInt(int n);
int getInt();
};
class DerivedClass : public BaseClass
{
int j;
public:
void setJ(int n);
int mul();
};
void BaseClass::setInt(int n)
{
i = n;
}
int BaseClass::getInt()
{
return i;
}
void DerivedClass::setJ(int n)
{
j = n;
}
int DerivedClass::mul()
{
return j * getInt();
}
int main()
{
DerivedClass ob;
ob.setInt(10); // load i in BaseClass
ob.setJ(4); // load j in DerivedClass
cout << ob.mul(); // displays 40
return 0;
}
|
A simple stack example: push, empty, pop and top
|
#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<char> stackObject;
stackObject.push('A');
stackObject.push('B');
stackObject.push('C');
stackObject.push('D');
while(!stackObject.empty())
{
cout << "Popping: ";
cout << stackObject.top() << endl;
stackObject.pop();
}
return 0;
}
|
A Static member variable example.
|
#include <iostream>
using namespace std;
class myclass
{
static int i;
public:
void setInt(int n)
{
i = n;
}
int getInt()
{
return i;
}
};
int myclass::i; // Definition of myclass::i. i is still private to myclass.
int main()
{
myclass object1, object2;
object1.setInt(10);
cout << "object1.i: " << object1.getInt() << '\n'; // displays 10
cout << "object2.i: " << object2.getInt() << '\n'; // also displays 10
return 0;
}
|