#include <iostream.h>
int main()
{
const float PI=3.14159;
float radius = 5;
float area;
area = radius * radius * PI; // Circle area calculation
cout << “The area is “ << area << “ with a radius of 5.\n”;
radius = 20; // Compute area with new radius.
area = radius * radius * PI;
cout << “The area is “ << area << “ with a radius of 20.\n”;
return 0;
}
#include <iostream.h>
int main()
{
const int age=35;
cout << age << “\n”;
age = 52;
cout << age << “\n”;
return 0;
}
void main()
{
short i = -3;
unsigned short u;
cout<< sizeof(i)<< &i;
cout<<sizeof(u)<<&u;
cout << (u = i) << "\n";
}
void main()
{
byte i = 125*4/10;
cout << i << "\n";
}
#include <iostream.h>
int main()
{
int f, g;
g = 5;
f = 8;
if ((g = 25) || (f = 35))
cout << “g is “ << g << “ and f got changed to “ << f;
return 0;
}
#include <iostream.h>
void main()
{
if (!0)
{ cout << “C++ By Example \n”; }
int a = 0;
if ( a !=0 && 2/a >0 )
cout<< “hello”;
}
![]() Figure 1 |
// BEEP : ‘\x07’
#include <iostream.h>
#define BEEP cout << “\a \n”
int main()
{
int num;
cout << “Please enter a number “;
cin >> num;
if (num == 1)
{ BEEP; }
else if (num == 2)
{ BEEP; BEEP; }
else if (num == 3)
{ BEEP; BEEP; BEEP; }
else if (num == 4)
{ BEEP; BEEP; BEEP; BEEP; }
else if (num == 5)
{ BEEP; BEEP; BEEP; BEEP; BEEP; }
return 0;
}
#include <iostream.h>
#define BEEP cout << “\a \n”
int main()
{
int num;
cout << “Please enter a number “;
cin >> num;
switch (num)
{
case (1): { BEEP;
break; }
case (2): { BEEP; BEEP;
break; }
case (3): { BEEP; BEEP; BEEP;
break; }
case (4): { BEEP; BEEP; BEEP; BEEP;
break; }
case (5): { BEEP; BEEP; BEEP; BEEP; BEEP;
break; }
}
return 0;
}
if (num == 1)
{cout << “Alpha”; }
else if (num == 2)
{ cout << “Beta”; }
else if (num == 3)
{ cout << “Gamma”; }
else
{ cout << “Other”; }
int a = 1;
while (a < 4)
{
cout << “This is the outer loop\n”;
a++;
while (a <= 25)
{
break;
cout << “This prints 25 times\n”;
}
}
#include <iostream.h>
#include <iomanip.h>
void main()
{
float total_grade=0.0;
float grade_avg = 0.0;
float grade;
int grade_ctr = 0;
do
{
cout << “What is your grade? (-1 to end) “;
cin >> grade;
if (grade >= 0.0)
{
total_grade += grade; // Add to total.
grade_ctr ++;
} // Add to count.
} while (grade >= 0.0); // Quit when -1 entered.
grade_avg = (total_grade / grade_ctr);
cout << “\nYou made a total of “ << setprecision(1) <<
total_grade << “ points.\n”;
cout << “Your average was “ << grade_avg << “\n”;
if (total_grade >= 450.0)
cout << “** You made an A!!”;
return 0;
}
#include <iostream.h>
void main()
{
int outer, num, fact, total;
cout << “What factorial do you want to see? “;
cin >> num;
for (outer=1; outer <= num; outer++)
{
total = 1;
for (fact=1; fact<= outer; fact++)
{ total *= fact; }
}
cout << “The factorial for “ << num << “ is “ << total;
return 0;
}
for (ctr=10; ctr>=1; ctr-=3)
{ cout << ctr << “\n”; }
n =10;
i=1;
for (i = 0 ; i < n ; i++)
cout<< ++i<<endl;
for (i=1; i<=10; i++);
for (j=1; j<=5; j++)
{
if ( i == j )
continue;
else ( i>j)
break;
else
cout << i << j;
cout<< endl;
}
i=1;
start=1;
end=5;
step=1;
for (; start>=end;)
{
cout << i << “\n”;
start+=step;
end--;
}
![]() Figure 2 |
![]() Figure 3 |
#include <iostream.h>
const int NUM = 8;
void main()
{
int nums[NUM];
int total = 0; // Holds total of user’s eight numbers.
int ctr;
for (ctr=0; ctr<NUM; ctr++)
{
cout << “Please enter the next number...”;
cin >> nums[ctr];
total += nums[ctr];
}
cout << “The total of the numbers is “ << total << “\n”;
return;
}
char teams[] = {‘E’,’a’,’g’,’l’,’e’,’s’,’\0', ’R’, ‘a’,’m’,’s’,’\0'};
int grades[3][5] = {80,90,96,73,65,67,90,68,92,84,70, 55,95,78,100};
![]() Figure 4 |
![]() Figure 5 |
struct MonthDays
{
char name[10];
int days;
};
![]() Figure 6 |
int square(int a)
{
a = a*a;
return a;
}
int Intqrt(int num)
{
int i;
i = 1;
do
++ i
while i*i <= num;
return(i –1);
}
Figure 7 |
bool palindrome(char strg[])
{
int len, k, j;
len = strlen(strg);
k = len/2;
j = 0;
bool palin = true;
while ( j < k && palin)
if (strg[j] != strg[len -1-j])
palin = false;
else
++ j;
return (palin)
}
int gcd(int p, int q)
{
int r ;
if (( r = p%q == 0)
return q ;
else
return gcd(q, r) ;
}
# include <iostream.h>
void main()
{
byte* a;
long* b;
cout<<sizeof(a)<<endl;
cout<<sizeof(b)<<endl;
}
float pay;
float *ptr_pay;
pay=2313.54;
ptr_pay = &pay;
#include<iostream.h>
void main()
{
int a;
int *aPtr; // aPtr is a pointer to an integer
a = 7;
aPtr = &a; //aPtr set to address of a
cout << “The address of a is “ << &a
<< “\nThe value of aPtr is “ << aPtr;
cout << “\n\nThe value of a is “<< a
<< “\nThe value of *aPtr is “ << *aPtr
<< endl;
}
int ara[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *ip1, *ip2;
#include <iostream.h>
void swap(int a[], int *c1, int *c2, int *d1, int *d2);
void main()
{
int a[2], c1, c2,d1,d2;
int *x1, *x2, *y1, *y2;
a[0] = 1 ; a[1] =2;
c1 = 1; c2 =2;
d1 = 1; d2 =2;
x1 = &c1; x2 = &c2;
y1 = &d1; y2 = &d2;
swap(a, x1,x2,y1,y2);
cout<<a[0]<<a[1]<<” “
<< *x1<<*x2<<” ”
<<*y1<<*y2;
swap(a, x1,x2,y1,y2);
cout<<a[0]<<a[1]<<” “
<< *x1<<*x2<<” ”
<<*y1<<*y2;
}
void swap(int a[], int *c1, int *c2, int *d1, int *d2)
{
a[0] = 2 ; a[1] =1;
*c1=2, *c2 =1;
int* temp = d1;
d1 =d2;
d2 = temp;
}
int findMax(int * vals, int numEls)
{
int j, max = *vals;
for (j = 1; j < numEls; j++)
if (max < *(vals + j))
max = *(vals + j);
return max;
}
#include<iostream.h>
#include<string.h>
#define MAX 80
void str_output(char *);
int main()
{
char a[MAX], b[MAX];
cin.getline(a, MAX, '\n');
str_output(a);
cout << endl;
strcpy(b,a);
str_output(b);
cout<< endl;
return 0;
}
void str_output(char *ptr)
{
………..
}
struct Date
{
int month;
int day;
int year;
}
#include<iostream.h>
class student{
private:
long int rollno;
int age;
char sex;
float height;
float weight;
public:
void getinfo();
void disinfo();
};
void student::getinfo()
{
cout << " Roll no :";
cin >> rollno;
cout << " Age :";
cin >> age;
cout << " Sex:";
cin >> sex;
cout << " Height :";
cin >> height;
cout << " Weight :";
cin >> weight;
}
void student::disinfo()
{
cout<<endl;
cout<< " Roll no = "<< rollno << endl;
cout<< " Age =" << age << endl;
cout<< " Sex =" << sex << endl;
cout<< " Height =" << height << endl;
cout<< " Weight =" << weight << endl;
}
void main()
{
student a;
cout << " Enter the following information " << endl;
a.getinfo();
cout << " \n Contents of class "<< endl;
a.disinfo();
}
class Int{
private:
int idata;
public:
Int(){
idata=0;
cout<<"default constructor is called"<<endl;
}
Int(int d){
idata=d;
cout<<"constructor with argument is called"<<endl;
}
void showData(){
cout<<"value of idata: "<<idata<<endl;
}
};
void main()
{
Int i;
Int j(8);
Int k=10;
Int *ptrInt = new Int();
ptrInt->showData();
delete ptrInt;
}
#include <iostream.h>
void main()
{
int num;
cout<< “Please enter the numbers of input: ”
cin>>num;
int a = new int [num];
int total = 0; // Holds total of user’s eight numbers.
int ctr;
for (ctr=0; ctr<num; ctr++)
{
cout << “Please enter the next number...”;
cin >> a[ctr];
total += a[ctr];
}
cout << “The total of the numbers is “ << total << “\n”;
return;
delete [] a;
}
// IntArray.h
class IntArray
{
private:
int* data; //pointer to the integer array
int size;
public:
IntArray(int* d, int s);
void showList();
void showFirst( );
};
// IntArray.cpp
IntArray::IntArray(int* d, int s)
{
data = d;
size = s;
}
void IntArray::showList()
{
cout<<"Entire list:" <<endl;
for(int x = 0; x< size; x++)
cout<< data[x]<<endl;
cout<< "----------------------"<< endl;
}
void IntArray::showFirst()
{
cout<< "First element is ";
cout << data[0]<< endl;
}
class Int{
private:
int idata;
public:
Int(){
idata=0;
cout<<"default constructor is called"<<endl;
}
Int(int d=9){
idata=d;
cout<<"constructor with argument is called"<<endl;
}
void showData(){
cout<<"value of idata: "<<idata<<endl;
}
};
void main()
{
Int i;
Int j(8);
Int k=10;
}
class Vector{
private:
int *value;
int dimension;
public:
Vector(int d=0){
dimension=d;
if (dimension==0)
value=NULL;
else{
value=new int[dimension];
for (int i=0; i<dimension; i++)
value[i]=0;
}
}
void showdata(){
for (int i=0; i<dimension; i++)
cout<<value[i];
cout<<endl;
}
~Vector(){
if (value!=NULL)
delete value;
}
};
void main()
{
Vector v(5);
v.showdata();
Vector v2(v);
v2.showdata();
}
Vector(const Vector& v){
dimension = v.dimension;
value=new int[dimension];
for (int i=0; i<dimension; i++)
value[i]=v.value[i];
}
class some{ // code segment a
public:
~some() {
cout<<"some's destructor"<<endl;
}
};
void main() {
some s;
s.~some();
}
class some{ // code segment b
int *ptr;
public:
some(){
ptr= new int;
}
~some(){
cout<<"some's destructor"<<endl;
if (ptr!=NULL){
cout<<"delete heap memory"<<endl;
delete ptr;
}
}
};
void main()
{
some s;
// s.~some();
}
class Auto {
public:
Auto(char*, double);
displayAuto(char*, double);
private:
char* szCarMake;
double dCarEngine;
};
Auto::Auto(char* szMake, double dEngine){
szCarMake = new char[25];
strcpy(szCarMake, szMake);
dCarEngineSize = dCarEngine;
}
Auto::displayAuto(){
cout<< “The car make: “<< szCarMake<< endl;
cout<< “The car engine size: “<< dCarEngine<< endl;
}
void main(){
Auto oldCar(“Chevy”, 351);
Auto newCar(oldCar);
oldCar.displayAuto();
newCar.displayAuto();
}
![]() Figure 8 |
class Point{
private:
int color;
protected:
double x;
double y;
public:
Point(double x=0, double y=0){
this->x=x; this->y=y;
}
void move(double dx, double dy){
x=x+dx;
y=y+dy;
}
~Point(){
cout<<"Destructor Point called";
}
};
class Point_Derive1:public Point{
private:
double z;
public:
Point_Derive1();
void move(double dx, double dy, double dz);
~Point_Derive1();
};
class Point_Derive2:protected Point{
private:
double z;
public:
Point_Derive1();
void move(double dx, double dy, double dz);
~Point_Derive1();
};
class Point_Derive3:private Point{
private:
double z;
public:
Point_Derive1();
void move(double dx, double dy, double dz);
~Point_Derive1();
};
#include <iostream.h>
#include <math.h>
const double PI = 2.0 * asin(1.0);
// class declaration
class Circle
{
protected:
double radius;
public:
Circle(double = 1.0); // constructor
double calcval();
};
// implementation section for Circle
// constructor
Circle::Circle(double r)
{
radius = r;
}
// calculate the area of a circle
double Circle::calcval()
{
return(PI * radius * radius);
}
// class declaration for the derived class
// Cylinder which is derived from Circle
class Cylinder : public Circle
{
protected:
double length; // add one additional data member and
public: // two additional function members
Cylinder(double r = 1.0, double l = 1.0) : Circle(r), length(l) {}
double calcval();
};
// implementation section for Cylinder
double Cylinder::calcval() // this calculates a volume
{
return length * Circle::calcval(); // note the base function call
}
int main()
{
Circle circle_1, circle_2(2); // create two Circle objects
Cylinder cylinder_1(3,4); // create one Cylinder object
cout << "The area of circle_1 is " << circle_1.calcval() << endl;
cout << "The area of circle_2 is " << circle_2.calcval() << endl;
cout << "The volume of cylinder_1 is " << cylinder_1.calcval() << endl;
circle_1 = cylinder_1; // assign a cylinder to a Circle
cout << "\nThe area of circle_1 is now " << circle_1.calcval() << endl;
return 0;
}
Comments, questions, feedback, criticisms?