Showing posts with label c++. Show all posts
Showing posts with label c++. Show all posts

Wednesday, 9 April 2014

The Class Constructor

The Class Constructor:

A class constructor is a special member function of a 
class that is executed whenever we create new objects of 
that class.A constructor will have exact same name as the 
class and it does not have any return type at all, 
not even void. Constructors can be very useful for setting
 initial values for certain member variables.
Following example explains the concept of constructor:
#include <iostream>
 
using namespace std;
 
class Line
{
   public:
      void setLength( double len );
      double getLength( void );
      Line();  // This is the constructor
 
   private:
      double length;
};
 
// Member functions definitions including constructor
Line::Line(void)
{
    cout << "Object is being created" << endl;
}
 
void Line::setLength( double len )
{
    length = len;
}
 
double Line::getLength( void )
{
    return length;
}
// Main function for the program
int main( )
{
   Line line;
 
   // set line length
   line.setLength(6.0); 
   cout << "Length of line : " << line.getLength() <<endl;
 
   return 0;
}
When the above code is compiled and executed, it produces the 
following result:
Object is being created
Length of line : 6

Monday, 7 April 2014

C programming

An electric power distribution company charges its domestic consumers as follows:
 Consumption Units   Rate of Charge
 0-200     Rs.0.50 per unit
 201-400  Rs.100 plus Rs.0.65 per unit excess 200
 401-600  Rs.230 plus Rs.0.80 per unit excess of 400.
Write a C program that reads the customer number and power consumed and prints the amount to be paid by the customer. [Nov./Dec.-2005,Set-3, Unit-1, Q.No.-34]
Ans. : */
#include<stdio.h>
#include<conio.h>
void main()
{
int n, p;
float amount;
clrscr();
printf("Enter the customer number: ");
scanf("%d",&n);
printf("Enter the power consumed: ");
scanf("%d",&p);
if(p>=0 && p<=200)
 amount=p*0.50;
else if(p>200 && p<400)
 amount = 100+((p-200) * 0.65);
else if(p>400 && p<=600)
 amount = 230 + ((p-400) * 0.80);
printf("Amount to be paid by customer no. %d is Rs.:%5.2f.",n,amount);
getch();
}





Write a program to read the values of x, y and z and print the results of the following expressions in one line.


Ans. :
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
float a,b,c;
clrscr();
printf("Enter the values of x,y and z : ");
scanf("%d%d%d",&x,&y,&z);
a=(x+y+z)/(x-y-z);
b=(x+y+z)/3;
c=(x+y)*(x-y)*(y-z);
printf("a = %f\tb = %f\tc = %f",a,b,c);
getch();
}



Write a program to read a text file and convert the file contents  in capital (Upper-case) and write the contents in a output file.

Ans. :
Program to copy the contents of one file into another*/
#include<stdio.h>
#include<process.h>

 void main()
  {
FILE *fp1,*fp2;
char a;
clrscr();
fp1=fopen("d:\\dtp\\test.txt","r");
if(fp1==NULL)
 {
 puts("cannot open this file");
 exit(1);
 }
fp2=fopen("d:\\dtp\\test1.txt","w");
if(fp2==NULL)
 {
 puts("Not able to open this file");
 fclose(fp1);
 exit(1);
 }
 do
  {
  a=fgetc(fp1);
  a=toupper(a);

  fputc(a,fp2);
  }
  while(a!=EOF);
  fcloseall();
  getch();
}


Write a program to copy the contents of one file into another. [June-2005, Set-4, Unit-5, Q.No.-2]


Ans. :
Program to copy the contents of one file into another*/
#include<stdio.h>
#include<process.h>

 void main()
  {
FILE *fp1,*fp2;
char a;
clrscr();
fp1=fopen("d:\\dtp\\test.txt","r");
if(fp1==NULL)
 {
 puts("cannot open this file");
 exit(1);
 }
fp2=fopen("d:\\dtp\\test1.txt","w");
if(fp2==NULL)
 {
 puts("Not able to open this file");
 fclose(fp1);
 exit(1);
 }
 do
  {
  a=fgetc(fp1);
  fputc(a,fp2);
  }
  while(a!=EOF);
  fcloseall();
  getch();
}
- See more at: http://loadyoursystem.blogspot.in/p/myzone.html#sthash.Y0FTQf1v.dpuf

A Program to Calculate Factorial of a Number

A Program to Calculate Factorial of a Number

#include <iostream.h>

int fact (int i) {
  int result = 1;
  while (i > 0) {
    result = result * i;
    i = i-1;
  }
  return(result);
}

int main () {
  int n;
  cout << "Enter a natural number: ";
  cin >> n;
  while (n < 0) {
    cout << "Please re-enter: ";
    cin >> n;
  }
  cout << n << "! = " << fact(n) << endl;
  return(0);
}

Comment

Comment Box is loading comments...