Sunday, June 1, 2014

C++ Program to calculate area using inheritance

#include <iostream>
#include<conio.h>
using namespace std;

// Base class
class Sample
{
   public:
      void setWidth(int w)
      {
         width = w;
      }
      void setHeight(int h)
      {
         height = h;
      }
   protected:
      int width;
      int height;
};

// Derived class
class Rectangle: public Sample
{
   public:
      int getArea()
      {
         return (width * height);
      }
};

int main(void)
{
   Rectangle Rect;

   Rect.setWidth(5);

   Rect.setHeight(7);

   // Print the area of the object.

   cout << "Total area: " << Rect.getArea() << endl;

getch();
   return 0;
}



C++ Simple pointer program to print address and value

#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
     int a= 5;
 /* actual variable declaration */
 int *ptr;
 /* pointer variable declaration */
  ptr = &a;
   /* store address of a in pointer variable*/
   cout<<"Address of variable a="<< &a;
    cout<<"\nAddress stored in variable ptr="<<ptr;
     cout<<"\nValue of *ptr variable="<<*ptr;
     getch();
     return 0;
      }


C++ Program to print largest number by if else

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{

int a,b;//variable declaration
cout<<"Enter two numbers:";//prompt
cin>>a>>b;
if(a==b)
cout<<a<<"\n Both numbers are equal";
if(a>b)
cout<<a<<"\n is larger";
else
cout<<b<<"\n is larger";
getch();
}


C++ Program to find largest number using function

#include <iostream>
#include <conio.h>
using namespace std;
int max(int , int); // function declaration
int main ()
{
int x; // local variable declaration:
int y;
int ans;
cout<<"Enter the first value:";
cin>>x;
cout<<"Enter the second value:";
cin>>y;
ans = max(x, y); // calling a function to get max value.
cout << "Max value is : " << ans << endl;
getchar();
return 0;
}
int max(int num1, int num2) // function definition
{
int result; // local variable declaration
if (num1 > num2)
result = num1;
else
result = num2;
getchar();
return result;
}



Saturday, May 31, 2014

C++ Program which displays the radius ,area and color of the circle using constructor

#include <iostream>   
#include<conio.h>
#include <string>      // using string
using namespace std;

class Circle {
private:
double radius;      // Data member (Variable)
string color;       // Data member (Variable)

public:
   // Constructor with default values for data members
Circle(double r , string c ) {
radius = r;
color = c;
   }

double getRadius() {  // Member function (Getter)
return radius;
   }

string getColor() {   // Member function (Getter)
return color;
   }

double getArea() {    // Member function
return radius*radius*3.1416;
   }
};   // need to end the class declaration with a semi-colon
 // Test driver function
int main() {
   // Construct a Circle instance
   Circle c1(1.2, "blue");
cout<< "Radius=" << c1.getRadius() << " Area=" << c1.getArea()
<< " Color=" <<c1.getColor() <<endl;

   // Construct another Circle instance
   Circle c2(3.4,"Red"); // default color
cout<< "Radius=" << c2.getRadius() << " Area=" << c2.getArea()
<< " Color=" <<c2.getColor() <<endl;

   // Construct a Circle instance using default no-arg constructor
   Circle c3(5,"Yellow");      // default radius and color
cout<< "Radius=" << c3.getRadius() << " Area=" << c3.getArea()
<< " Color=" <<c3.getColor() <<endl;
getch();
return 0;
}










First Java Program

public class HelloWorld {
   public static void main(String[] args) {
      System.out.println("Hello, World");
   }
}

To compile and run java program open command prompt. Go to the location where the java file is saved. To complie type javac and name of java program. To run java program type java and name of file. In below picture "FirstJava" file is compiled and run by cmd prompt.

C++ Program to print week day according to number by getting number from user

#include<conio.h>
#include<iostream.h>
main()
{
      int num;
      clrscr();
      cout<<"Enter any number between 1 to 7: ";                            
      cin>>num;
      switch(num)
      {
      case 1:
                   cout<<"Monday";
                   break;
      case 2:
                   cout<<"Tuesday";
                   break;
      case 3:
                   cout<<"Wednesday";
                   break;
      case 4:
                   cout<<"Thursday";
                   break;
      case 5:
                   cout<<"Friday";
                   break;
      case 6:
                   cout<<"Saturday";
                   break;
      case 7:
                   cout<<"Sunday";
                   break;
      default:
                   cout<<"INVALID NUMBER!!! Please enter number between 1 to 7";
      }
      getch();
      }