Thursday, August 20, 2015

BCA III Phase I of the termwork.

PHASE I
Q1.WAP to design a class RECTANGLE, which has data members like len,width,ar,peri. It It should have some member functions like getdata(), dispdata(),cal_area() & calc_peri().
Q2.WAP to design a class Triangle, which has data members like side_a,side_b,side_c,ar,peri. It It should have some member functions like getdata(), dispdata(),cal_area() & calc_peri().
Q3. WAP to demonstrate the classes Square, Triangle & Circle in a same program .[ HINT: USE of Scope Resolution Operator].
Q4. WAP to demonstrate a class STUDENT which has data members like rno, age, marks, name. It should have some member functions like getdata(), dispdata(),cal_total(), etc.
Q5. WAP to demonstrate a class INTEGER which has data members like num1, num2, result. It should have some member functions like getdata(), dispdata(),add(), sub(),mult(),div().
Q6. WAP to demonstrate a class INTEGER_CALC which has data members like num. It should have some member functions like getdata(), dispdata(),is_even(), is_prime(), is_palindrome( ),power(),fact(), reverse(),sum_of_disits()etc.
Q7. WAP to demonstrate a class COMPLEX, which has two data members of int type. It should have some member functions like getdata(), dispdata(),add(), sub(), prod() etc.
Q8. Create a class called user_time that has separate integer members for hours, minutes and seconds. One member function should initialize this data to 0, and another should initialize it to fixed values. A member function should display it in HH:MM:SS format. The final two member functions should add and subtract two objects of time passed as arguments. A main () program should create two initialized user_time objects. Then it should add the two initialized together, leaving the result in the third user_time object. Finally it should display the value of third variable.
Q9. Create a class called Distance that has separate member data inches and feet. One member function should initialize this data to 0, and another should initialize it to the values entered by user. A member function should display it. The member function should add two objects of type distance passed as arguments.[ 1 feet =12 inches]
Q10. Explain the concept of this pointer with the help of examples.
Q11. WAP to demonstrate the use of an array inside a class( eg Marks of student).
Q12. WAP to demonstrate the use of an array of objects in the main() function.


NOTE:Write Question in Black ink... Solution in blue ink &  Output shuld be Printed.

Wednesday, August 19, 2015

this pointer:Part (C)

#include <iostream>
 

class Box
{
   public:
      // Constructor definition
      Box(double l=2.0, double b=2.0, double h=2.0)
      {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
      }
      double Volume()
      {
         return length * breadth * height;
      }
      int compare(Box box)
      {
         return this->Volume() > box.Volume();
      }
   private:
      double length;     // Length of a box
      double breadth;    // Breadth of a box
      double height;     // Height of a box
};

int main(void)
{
   Box Box1(3.3, 1.2, 1.5);    // Declare box1
   Box Box2(8.5, 6.0, 2.0);    // Declare box2

   if(Box1.compare(Box2))
   {
      cout << "Box2 is smaller than Box1" <<endl;
   }
   else
   {
      cout << "Box2 is equal to or larger than Box1" <<endl;
   }
   return 0;
}
When the above code is compiled and executed, it produces the following result:
Constructor called.
Constructor called.
Box2 is equal to or larger than Box1

this pointer: Part (B)

2. To return reference to the calling object
/* Reference to the calling object can be returned */
Test& Test::func ()
{
   // Some processing
   return *this;
}
When a reference to a local object is returned, the returned reference can be used to chain function calls on a single object.
#include<iostream>

 
class Test
{
private:
  int x;
  int y;
public:
  Test(int x = 0, int y = 0) { this->x = x; this->y = y; }
  Test &setX(int a) { x = a; return *this; }
  Test &setY(int b) { y = b; return *this; }
  void print() { cout << "x = " << x << " y = " << y << endl; }
};
 
int main()
{
  Test obj1(5, 5);
 
  // Chained function calls.  All calls modify the same object
  // as the same object is returned by reference
  obj1.setX(10).setY(20);
 
  obj1.print();
  return 0;
}