"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain
Showing posts with label Classes and Objects. Show all posts
Showing posts with label Classes and Objects. Show all posts

Sunday, April 25, 2010

Write a class to represent a vector (1 - D Numeric array). Include member functions :

*   for vector creation.
*   for modification of a given element
*   for displaying the largest value in the vector
*   for displaying the entire vector
*   Write a program using this class.



#include<iostream.h>
#include<conio.h>

class vec
{
int arr[10];

public :

vec() // default constructor
{
for(int i = 0 ; i < 10 ; i++)
 arr[i] = 0;
}

vec(int a[])  // parameterised constructor
{
for(int i=0; i < 10 ; i++ )
{
arr[i] = a[i];
}
}

~vec()  // destructor
{
delete arr;
}

void  crea()
{
for(int i=0; i < 10 ; i++ )
{
cout << "\n Enter an element : ";
cin >> arr[i];
}
}

void modi(int pos, int ele)
{
arr[pos] = ele;
}

void disp()
{
cout<< "\n The elements of the array --> ";
for(int i = 0; i<10; i ++)
{
cout<< arr[i] << " ";
}
}

int disp_lar()
{
int lar = arr[0];
for(int i =1; i < 10 ; i++)
{
if ( lar < arr[i] )
{ lar = arr[i];
}
}
return lar;
}

}; // end of class

void main()
{
vec v1; // object v1 is created using default cons
v1.crea();
v1.disp();
int max = v1.disp_lar();
cout<< "\n The max element in the array is " << max;

cout<<"\n\n Enter 1 for modifying the array element :";
cout<<"\n Enter 2 for not modifying the array :";

int ch, ps, el;
cout<< "\n Enter your choice :";
while(ch == 1)
{
cout<< "\n Enter the position to be changed";
cin>>ps;
cout<< "\n Enter the element to be entered ";
cin >> el;
v1.modi(ps, el);
cout<< "\n Enter your choice :(1 or 2 )";
cin >> ch;
}

cout<< "\n The elements after modification ";
v1.disp();
cout<< "\n The Max element is ";
max = v1.dis_lar();
cout << max ;

getch();

}

Tuesday, March 23, 2010

Declare a class to represent fixed-deposit account of 10 customers with the following data members: Name of the depositor, Account Number, Time Period ( 1 or 3 or 5 yrs), Amount. The class also contains following member functions : (A) To initialise data members. , (B) For withdrawal of money (after half or the time period has passed ). , (C) To display the data members. Write a program to handle 10 account holders. Make necessary changes in the class definition - if required.


#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>

class account
{
private:

char name[20];
long acno;
int time;
double amount;

public :
void get_data( char nm[], long n, double am)
{
strcpy(name, nm);
acno = n;
time  = 1;
cout<< "\n Enter Duration ( for 3 yrs / 5 yrs) ..... \n else duration will be considered as 1 yr. ";
int t;
cin>> t;
if(t == 3 || t == 5)
  time = t;
amount = am;
}

void withdrw()
{
cout<< "\n Enter the time period,that has passed after creating new fixed-deposit A/c :"
int a;
cin>>a;
cout<< "\n The amount to be drawn :";
double d;
cin>>d;

if(a > = time/2)
   {
    if(d <= amount) 
         amount = amount - d;
     else
         cout<<"\n Balance is less in your account.Abort !";
    }
else
         cout<<"\n Money can not be withdrawn. For details contact helpline.";
}
void display()
{
cout<< "A/C No." << "\t" << "Name " << "\t\t" << "Amount" << "\n\n";
cout << acno << "\t" << name << "\t\t" << amount << "\n\n";
}

void display_time()
{
cout<< "\n A/c Duration = " << time << "yrs";
}

};

void main()
{
account customer[10];
char ch= 'y';

char nam[20];
long acn;
double amt;

/* Input 10 customer's details  */
for(int i = 0; i < 10 ; i++)
{
cout<< "\n Do you want to create a new account. Minimum duration is 1 yr":
cout<< "Enter (y /n) :";
cin >> ch;

if(tolower(ch) ! = 'y')
          { i--;
            continue;
          }
cout<< "\n Enter name :";
gets(nam) ;
cout<< "\n Enter A/c No. ";
cin>> acn;
cout<< "\n Enter amount ";
cin>> amt;

customer[i].get_data(nam, acn, amt);
}

/* Display 10 customer's details */
for(i = 0; i<10; i++)
{
customer[i].display();
}

/* withdraw money */
cout<< "\n************** Do you Withdraw Money ........\n";
cout<< "\n You can withdraw money only when, after creating your A/c ,half of the time period has passed . ";
cout<< "\n Enter A/c No.";
cin>>acn;
for(i = 0 ; i <10 ; i++)
{
if (acn == customer[i].acno)
      customer[i].withdrw();
}

 
/* Display 10 customer's details */
for(i = 0; i<10; i++)
{
customer[i].display();
}

getch();
}

Write a C++ program to implement a circle class. Each Object of this class will represent a circle, storing its radius and the x and y coordinate of its center as floats. Include two access functions for: (a) Calculate area of circle , (b) calculating the circumference of circle.


#include<iostream.h>
#include<conio.h>
// start class
class circle
{

private :

float radius;
float x , y;

public :

void get_radius(float r)
{ radius = r; }

void get_xycoor(float a, float b)
{ x = a;
y = b;
}

void area( )
{
cout<< "\n\n The area of the circle of radius "<< radius <<" is " << 3.14 * radius * radius ;
}

void circum()
{
cout<< "\n\n The circumference of the circle of radius "<< radius <<" is " << 2 *3.14 * radius ;
}
};
//End of class

void main()
{
circle c1;
float rad;
cout<< "\n Enter radius :";
cin >> rad;
c1.get_radius(rad);
c1.area();
c1.circum();
getch();
}
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.