Saturday, June 25, 2011



                                         

Assignment

Object oriented programming


Que-1-
Abstract class's can have a constructor, but you cannot access it through the object, since you cannot instantiate abstract class. To access the constructor create a sub class and extend the abstract class which is having the constructor. Example public abstract class AbstractExample { public AbstractExample(){ System.out.println("In AbstractExample()"); } } public class Test extends AbstractExample{ public static void main(String args[]){ Test obj=new Test(); } } If interface & abstract class have same methods and those methods contain no implementation, which one would you prefer? Obviously one should ideally go for an interface, as we can only extend one class.
Because abstract classes should never be instantiated, it is important to correctly define their constructors. It is also important to ensure that the functionality of your abstract class is correct and easily extended. If you define a protected constructor in an abstract class, the base class can perform initialization tasks when instances of a derived class are created. An internal constructor prevents the abstract class from being used as the base class of types that are not in the same assembly as the abstract class. Constructors with public or protected internal visibility are for types that can be instantiated. Abstract types can never be instantiated.
Que-2-
#include<conio.h>
#include<iostream.h>

#include<stdlib.h>


int main()
{

    int *ptr,i,n;
    clrscr();

    cout<<"Enter the no of elements:";
    Cin<<&n;

    ptr=(int *)malloc(sizeof(int)*n);
    if(ptr==NULL)

    {
        Cout<<"Not enough memory";

        exit(1);
    }

    for(i=0; i<n; i++)
    {

        cout<<"Enter  element : "<<i+1;
        cin<<&ptr[i];

    }
    cout<<"Array in original order\n";

    for(i=0; i<n; i++)
    {

        Cout<<"\n"<<ptr[i]);
    }

    cout<<"Array in reverse order\n";
    for(i=n-1; i>=0; i--)

    {
        Cout<<ptr[i]);

    }
    getch();

    return 0;
}


Que-3-
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int search_name(const string find_name[], const string in_name, int size1);
// Function to search for an inputted student name in the name[] array

int main()
{
               int size, *test1, *test2, *test3, score1, score2, score3, choice2;
               char choice, dummy;
               string *name, student, find_student;
               ifstream infile;
               ofstream outfile;

               cout << "hello\n";

               infile.open("scores.txt");
               if (infile.fail())
               {
                               cout << "Cannot open data file\nProgram halted\n\n";
                               exit(1);
               }

                               infile >> size;

                               test1 = new int[size];
                               test2 = new int[size];
                               test3 = new int[size];
                               name = new string[size];

                               for (int i = 0; i < size; i++)
                               {
                                              infile >> student >> score1 >> score2 >> score3;
                                              name[i] = student;
                                              test1[i] = score1;
                                              test2[i] = score2;
                                              test3[i] = score3;
                               }

               do
               {
                               system("cls");
                               cout << "Welcome to Test Score 2000\n\n\n"
                                               << "Please choose from the following:\n\n"
                                              << "A.  Display all student scores for a single test\n"
                                              << "B.  Display all scores and an average for a single student\n"
                                              << "C.  Display class average for each test along with the highest and lowest score\n"
                                              << "Q.  End Program\n\n"
                                              << "<=SELECTION=> ";
                               cin >> choice;
                               choice = toupper(choice);

                               switch (choice)
                               {
                                              case 'A':
                                                             {
                                                                            do
                                                                            {
                                                                                            system("cls");
                                                                                            cout << "Please input which test scores you would like to view - 1, 2, or 3.\n"
                                                                                                            << "Input 0 to return to main menu ==> ";
                                                                                            cin >> choice2;
              
                                                                                            switch (choice2)
                                                                                            {
                                                                                                           case 1:
                                                                                                                          {
                                                                                                                                         
cout << "\nTest " << choice2 << endl
                                                                                                                                                         << "========================================\n";
                                                                                                                                         
for (int i = 0; i < size; i++)
                                                                                                                                                        
{
                                                                                                                                                                       
cout << name[i] << "\t\t" << test1[i] << endl << endl;
                                             
                                                                                                           }
                                                                                                                                         
cout << "\n\nHit Enter to continue....";
                                                                                                                                          cin.ignore(100,'\n');
                                                                                                                                          cin.get(dummy);

break;
}
                                                                           
case 2:
                                                             {
                                                                                                                                         
cout << "\nTest " << choice2 << endl
                                                                                                                                                          << "========================================\n";


                                                                                                                                         
for (int i = 0; i < size; i++)
                                                                                                                                          {
                                                                                                                                                         cout << name[i] << "\t\t" << test2[i] << endl << endl;
                                                                                                                                          }

                                                                                                                                         
cout << "\n\nHit Enter to continue....";
                                                                                                                                          cin.ignore(100,'\n');
                                                                                                                                          cin.get(dummy);
                                             
                               break;
                                                                                                                          }
              
                                                                                                          
case 3:
                                                                                                           {
               cout << "\nTest " << choice2 << endl
                                                                                                                                                          << "========================================\n";
               for (int i = 0; i < size; i++)
                                                                                                                                          {
                                                                                                                                                         cout << name[i] << "\t\t" << test3[i] << endl << endl;
                                                                                                                                          }
               cout << "\n\nHit Enter to continue....";
                                                                                                                                          cin.ignore(100,'\n');
                                                                                                                                          cin.get(dummy);
                               break;
                                                                                                                          }

                               case 0:
               break;
              
               default:
                                                                                            {
               cout << "\nIncorrect input, please try again!\n";
               out << "\n\nHit Enter to continue....";
                                                                                                                          cin.ignore(100,'\n');
               cin.get(dummy);
                                                                                                                          }
                                                                                            }
                                                                                           
                                                                            } while (choice2 != 0);
                                                             }
                                                             break;
                                             
                                              case 'B':
                                                             {
                                                                            do
                                                                            {
                                                                                            system("cls");
                                                                                            int i;
               cout << "Please choose from the following students or input QQQ to return:\n\n";

               for (int num = 0; num < size; num++)
                                              {
               cout << name[num] << endl;
                                                                                            }
               cout << "\n==> ";
                               cin >> find_student;
                               i = search_name(name, find_student, size);
                                                                           
                                                                                            if (i < 0)
                               cout << "Name not found, please try again!\n";
                                                                                                           else
                                                                                            {
                               cout << "\nStudent\tTest 1\tTest 2\tTest 3\tAverage\n"
                                                                                                                           << "======================================="
                                                                                                                           << endl << name[i] << "\t" << test1[i] << "\t" << test2[i]
                                                                                                                            << "\t" << test3[i] << "\t" << ((test1[i] + test2[i] + test3[i]) / 3) << endl << endl;
                                                                                            }
                                                                            cout << "\n\nHit Enter to continue....";
                                                                            cin.ignore(100,'\n');
                                                                            cin.get(dummy);
                                                                            } while (find_student != "QQQ" || find_student != "qqq");
                                                             }
                                                             break;

                                              case 'C':
                                                             {
                                                                            system("cls");
                                                                            int average1 = 0, average2 = 0, average3 = 0, hinum1, lownum1, hinum2, lownum2, hinum3, lownum3, max1 = 0, max2 = 0, max3 = 0,
                                                                                            min1 = 100, min2 = 100, min3 = 100;
                                                                            for (int i = 0; i < size; i++)
                                                                            {
                                                                                            average1 += test1[i];
                                                                                            average2 += test2[i];
                                                                                            average3 += test3[i];
                                                                                            hinum1 = test1[i];
                                                                                            hinum2 = test2[i];
                                                                                            hinum3 = test3[i];
                                                                                            lownum1 = test1[i];
                                                                                            lownum2 = test2[i];
                                                                                            lownum3 = test3[i];

                                                                                            if (hinum1 > max1)
                                                                                                           max1 = hinum1;
                                                                                            if (hinum2 > max2)
                                                                                                           max2 =hinum2;
                                                                                            if (hinum3 > max3)
                                                                                                           max3 = hinum3;
                                                                                            if (lownum1 < min1)
                                                                                                           min1 = lownum1;
                                                                                            if (lownum2 < min2)
                                                                                                           min2 = lownum2;
                                                                                            if (lownum3 < min3)
                                                                                                           min3 = lownum3;
                                                                            }

                                                                            cout << "\nTest\tAverage\t\tHigh Score\tLow Score\n"
                                                                                             << "=================================================\n"
                                                                                             << "1\t" << (average1 / size) << "\t\t" << max1 << "\t\t" << min1 << endl
                                                                                             << "2\t" << (average2 / size) << "\t\t" << max2 << "\t\t" << min2 << endl
                                                                                             << "3\t" << (average3 / size) << "\t\t" << max3 << "\t\t" << min3 << endl << endl;
                                                             }
                                                             cout << "\n\nHit Enter to continue....";
                                                             cin.ignore(100,'\n');
                                                             cin.get(dummy);
                                                             break;
                               }
               }while (choice != 'Q');

               return 0;
}

int search_name(const string find_name[], const string in_name, int size1)
{
               int index = 0, not_found = -1;

               while (in_name != find_name[index] && index <= size1)
                               index++;

               if (in_name != find_name[index] && index > size1)
                               return not_found;
               else
                               return index;
}

Que-4-
Many programs have little need for memory management; they use a fixed amount of memory, or simply consume it until they exit. The best that can be done for such programs is to stay out of their way. Other programs, including most C++ programs, are much less deterministic, and their performance can be profoundly affected by the memory management policy they run under. Unfortunately, the memory management facilities provided by many system vendors have failed to keep pace with growth in program size and dynamic memory usage.
Because C++ code is naturally organized by class, a common response to this failure is to overload member operator new for individual classes. In addition to being tedious to implement and maintain, however, this piece-meal approach can actually hurt performance in large systems. For example, applied to a tree-node class, it forces nodes of each tree to share pages with nodes of other (probably unrelated) trees, rather than with related data. Furthermore, it tends to fragment memory by keeping large, mostly empty blocks dedicated to each class. The result can be a quick new/delete cycle that accidentally causes virtual memory thrashing. At best, the approach interferes with system-wide tuning efforts.
For ex.-
  class Heap {
   protected:
    virtual ~Heap();
   public:
    virtual void* allocate(size_t) = 0;
    static Heap& whatHeap(void*);
  };
(The static member function whatHeap(void*) is discussed later.) Heap's abstract interface is simple enough. Given a global Heap pointer, the regular global operator new can use it:

  extern Heap* __global_heap;

  inline void*
  operator new(size_t sz)
    { return ::__global_heap->allocate(sz); }
Inline dispatching makes it fast. It's general too; we can use the Heap interface to implement the placement operator new, providing access to any private heap:
  inline void*
  operator new(size_t size, Heap& heap
    { return heap.allocate(size); }
What kind of implementations might we define for the Heap interface? Of course the first must be a general purpose memory allocator, class HeapAny. (HeapAny is the memory manager described in detail in the second half of this article.) The global heap pointer, used by the regular operator new defined above, is initialized to refer to an instance of class HeapAny:
  extern class HeapAny __THE_global_heap;
  Heap* __global_heap = &__THE_global_heap;
Users, too, can instantiate class HeapAny to make a private heap:
  HeapAny& myheap = *new HeapAny;
and allocate storage from it, using the placement operator new:
  MyType* mine = new(myheap) MyType;
As promised, deletion is the same as always:
  delete mine;
Now we have the basis for a memory management architecture. It seems that all we need to do is provide an appropriate implementation of class Heap for any policy we might want. As usual, life is not so simple.
Que-6-
Here are two possible solutions that define why How do the following two statements differ in operation?
cin>>c;
cin.get(c);

declare first and last to be quite large:
char first[256], last[256];
cin.getline(first, sizeof(first));
cin.getline(last, sizeof(last));
Or, in the Standard Template Library (STL) there is a string class that helps with this sitution a lot:
string first, last;
getline(cin,first);
getline(cin, last);

1 comment:

  1. The Most Popular Slots in Europe
    The most popular 마라톤벳 slots online are Pragmatic Play, Wolf Gold, and many more. 11토토 With some of the highest house edge in Europe, this 온라인 포커 추천 online slot 윈윈벳먹튀 offers a 슬롯 나라 great

    ReplyDelete