Structures and Classes are very important data structures used in C / C++. You as a programmer has a choice to use any of them as Structures and Classes do provide features some what similar but only with some differences. Now you are given a task to code either using Structures or Classes. Please give your justification in favor or against of any one you will use for coding.
Your opinion should be concrete with justification in 5 -7 lines only. While giving your opinion in favor or against don't write detailed pages or paragraphs as it is discourage your answer should be to the point.
Student's Message:
"I will prefer to use class over structures in my program:
Reason:
Structures has only data variables, grouped and named as a single actor,
but in classes there are both data members as well as data functions to manipulate data members.
Classes are much variable then structures. classes lets code to encapsulate when need it increase the information hiding and controls abstract objects more efficiently. classes can also deals with good structural function.
In structures data variables are by default public and are visible. But classes in the data members and member functions are by default are private and are not visible."
/*
This program prints Electricity Bill from a txt file �Electricity_Bill� which contains the bill information
*/
#include <iostream.h>
#include <fstream.h>
main()
{
ifstream inFile; // Handle for the input file
char inputFilename[] = "Electricity_Bill.txt"; // file name, this file is in the current directory
const int MAX_CHAR_TO_READ = 2000; // maximum character to read in one line
char completeLineText[MAX_CHAR_TO_READ]; // to be used in getLine function
inFile.open(inputFilename); // opening the file
// checking that file is successfuly opened or not
if (!inFile)
{
cout << "Can't open input file named " <<inputFilename << endl;
exit(1);
}
while (!inFile.eof())
{
inFile.getline(completeLineText, MAX_CHAR_TO_READ);
cout << completeLineText << endl;
}
cout<<endl;
cout<<endl;
cout<<endl;
inFile.close();
system("pause");
return 0;
}
Electricity Consumer Bill
------------------------------------------------------------------------------
Reference No Tariff Load Old A/C No
123456789123456 2 1 123456789123456
-------------------------------------------------------------------------------
Name and Address
XYZ Lahore Pakistan
--------------------------------------------------------------------------------
Reading MF Total Unit Consumed Total Cost of electricity
55671 1 328 9999
-------------------------------------------------------------------------------
Month Units Bill Current Bill 10732
Jan-11 312 5000 Arrears 0
Feb-11 312 5000 Tariff Subsidy NA
Mar-11 312 5000 Payable within Duedate 10732
using namespace std;
#include<iostream>
main()
{
int days;
int Rent_amount;
char carNo[10]; // chracter sting
char cust_name[20]; // chracter string
char model; // to be used for car model selection choice
int calc_rent(char, int); // Rent Ammount calculation Funciton
cout<<"Please provide cumstomer Name\t";
cin>>cust_name;
cout<<"\nPlease provide Car Model"<<endl;
cout<<"\tEnter 'A' for Model 2009.\n\t Eenter 'B' for Model 2010\n\t Enter 'C' for Model 2011\n";
cin>>model;
cout<<"Please enter car Number = \t";
cin>>carNo;
cout<<"Please enter Number of days = \t";
cin >> days;
// Swithc structure for opitons selection
switch(model)
{
case 'A':
case 'a':
cout<<"\n\n\tCustomer Name:" <<cust_name;
cout<<"\n\tCar Model:2009";
cout<<"\n\tCar No:"<<carNo;
cout<<"\n\tNumber of days:"<<days;
Rent_amount = calc_rent(model, days);
cout << "\n\tYour Rental Amaount is:"<<Rent_amount<< endl;
break;
case 'B':
case 'b':
cout<<"\n\n\tCustomer Name:" <<cust_name;
cout<<"\n\tCar Model:2010";
cout<<"\n\tCar No:"<<carNo;
cout<<"\n\tNumber of days:"<<days;
Rent_amount = calc_rent(model, days);
cout << "\n\tYour Rental Amaount is:"<<Rent_amount<< endl;
break;
case 'C':
case 'c':
cout<<"\n\n\tCustomer Name:" <<cust_name;
cout<<"\n\tCar Model:2011";
cout<<"\n\tCar No:"<<carNo;
cout<<"\n\tNumber of days:"<<days;
Rent_amount = calc_rent(model, days);
cout << "\n\tYour Rental Amaount is:"<<Rent_amount<< endl;
break;
default:
cout<<"Please Enter Correct Model Name using A to C \n";
}
system("pause"); // causes the prompt to pause
}
int calc_rent(char model, int days) // Rent Calculation Function
{
if(model == 'A' || model == 'a')
return (5000 * days);
else if(model == 'B' || model == 'b')
return (8000 * days);
else if(model == 'C' || model == 'c')
return (10000 * days);
}
#include<iostream.h>
// ---------- declaration of user defined function for merging and sorting array -----------
//user defined function for merging the arrays into one array
void merge(int [][3], int [][3], int[]);
// user-defined function for sorting the merged array
void sort(int[], int);
//-------------- Start of the main function -------------------
main()
{
int A[3][3],B[3][3]; // declaration of 2D arrays
int C[18]; // declaration of 1D array to store the values after merging
// prompting the user to enter the elements of first array
cout<<"Please enter the Elements of 1st Matrix\n\n";
for(int row=0;row<3;row++)
{
for(int col=0;col<3;col++)
{
cout <<"Enter the element for row "<<row+1 << ", column " <<col+1 <<":\t";
cin>>A[row][col];
}
}
// prompting the user to enter the elements of second 2d array
cout<<"\n\nPlease enter the Elements of 2nd Matrix\n\n";
for(int row=0;row<3;row++)
{
for(int col=0;col<3;col++)
{
cout <<"Enter the element for row "<<row+1 << ", column " <<col+1 <<":\t";
cin>>B[row][col];
}
}
cout<<"\n\nElements of 1st Matrix are:\n\n";
for(int row=0;row<3;row++)
{
for(int col=0;col<3;col++)
{
cout<<A[row][col];
cout<<"\t";
}
cout<<endl;
}
cout<<"\n\nElements of 1st Matrix are:\n\n";
for(int row=0;row<3;row++)
{
for(int col=0;col<3;col++)
{
cout<<B[row][col];
cout<<"\t";
}
cout<<endl;
}
// calling user defined function for merging arrays
merge(A, B, C);
// calling user defined function for sorting the resultant array
sort(C, 18);
// displaying the elements of sorted array on screen
cout<<"\n\nSorted Values from Both the Matrices are:\n\n";
for(int i=0; i<18; i++)
{
cout<<C[i];
cout<<"\t";
}
cout<<endl<<endl;
// function to halt the output screen
system("pause");
}
// function definition for merging the arrays
void merge(int Array1[][3], int Array2[][3], int mergeArray[])
{
int insert = 0;
for(int row=0; row<3; row++)
{
for(int col=0; col<3;col++)
{
mergeArray[insert]=Array1[row][col];
insert++;
}
}
for(int row=0; row<3; row++)
{
for(int col=0; col<3; col++)
{
mergeArray[insert]=Array2[row][col];
insert++;
}
}
}
void sort(int Array[], int size)
{
for(int row=0; row<size; row++)
{
for(int col=row; col<size; col++)
{
if(Array[row]<Array[col])
{
int t;
t = Array[row];
Array[row] = Array[col];
Array[col] = t;
}
}
}
}
/* Solution Assignment No. 3, Spring 2011 */
// This program calculate the area and circumference of the circle by declaring a class
// Including header files in the program.
#include <iostream>
using namespace std;
// defining a macro for the value of PI
#define PI 3.14
// Beginning of the class Circle
class Circle {
// declaring public members of the class
public:
double radius; // only data member of the class
// declaration of public member functions of the class
Circle(); // default constructor
Circle(double); // Parameterized constructor
double CalculateArea(); // calculates area of the circle and then returns
double CalculateCircum();// calculates circumference of the circle
void Display(); // displays the area and circumference of the screen
};
// end of class
// definition of default constructor of class
Circle::Circle()
{
radius = 0.0; // assignning default value to the data member radius
}
// definition of parameterized constructor of the class
Circle::Circle(double rad)
{
radius = rad;
}
// definition of the function for area calculation of the circle
double Circle::CalculateArea()
{
return (PI * radius * radius); // calculating area and then returning it the caller function
}
// definition of the function for calculating circumference of the circle
double Circle::CalculateCircum()
{
return ( 2 * PI * radius ); // calculating and returning the circumferencr to the caller function
}
// defintion of display() function
void Circle::Display()
{
cout<<"\nRadius of Circle:\t\t"<<radius;
cout<<endl<<endl;
cout<<"Area of Circle:\t\t\t"<<CalculateArea();
cout<<endl<<endl;
cout<<"Circumference of Circle\t\t"<<CalculateCircum();
cout<<endl<<endl;
}
// beginning of the main() function
main()
{
Circle C1, C2(3.5); // declaring two objects of class student
// for each object created, displaying the information on the screen
C1.Display();
C2.Display();
system("pause"); // system function to halt the output screen
}
No comments:
Post a Comment