Friday, 9 June 2017

CBSE Class 12 - Computer Science - C++ Practical Class Design Snippet-2 (#cbseNotes)

C++ Practical
Class Design Snippet-2

CBSE Class 12 - Computer Science - C++ Practical Class Design Snippet-2  (#cbseNotes)

Question 2: Write a declaration for a class Person which has the following:
  • data members name, phone
  • set and get functions for every data member
  • a display function
  • a destructor

(i) For the Person class above, write each of the constructors, the assignment operator and the getName member functions. Use member initialization list as often as possible

(ii) Given the Person class above, write the declaration for the class Spouse that inherits from Person and does the following:
  • has an extra data member spouseName
  • redefines the display member function

(iii) For the Spouse class above, write each of the constructors and display member, functions. Use member initialization lists as often as possible.


Answer:




#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>

#define NAMELEN 40
#define PHONELEN 12

class Person
{
 protected:
  char name[NAMELEN];
  char phone[PHONELEN];
 
 public:
  Person()   // constructor
  {
   name[0] = '\0';
   phone[0] = '\0';
  } 
  
  Person(char pname[], char phnum[])
  {
   strcpy(name, pname);
   strcpy(phone, phnum);
  }
  
  void getName(char pname[])
  {
   strcpy(pname, name);
  }
  
  void getPhone(char phonenum[])
  {
   strcpy(phonenum, phone);
  }
  
  void setName()
  {
   cout << "Enter Person Name:";
   gets(name);
  }
  
  void setPhone()
  {
   cout << "Enter Phone:";
   gets(phone);
  }
  
  void Display()
  {
   cout << "Person Name:" << name << endl;
   cout << "Phone Number:" << phone << endl;
  }
  
  ~Person()
  {
   strcpy(name, "");
   strcpy(phone, "");
  }
};

class Spouse : public Person
{
 protected:
  char spouseName[NAMELEN];
 public:
  Spouse()
  {
   strcpy(spouseName, "");
  }
  
  Spouse(char pname[], char pnum[], char spname[])
  {
   strcpy(spouseName, spname);
   strcpy(name, pname);
   strcpy(phone, pnum);
  }
  
  void Display()
  {
   cout << "Person Name:" << name << endl;
   cout << "Phone Number:" << phone << endl;
   cout << "Spouse Name:" << spouseName << endl;
  }
};

void main()
{
 clrscr();
 Person obj1("Ricky Matrin", "9812345678");
 obj1.Display();
 obj1.setName();
 obj1.Display();

 Spouse obj2("John Lenon", "9988345601", "Monica Lenon");
 obj2.Display();
 getch();
}


CBSE Class 12 - Computer Science - C++ Practical Class Design Snippet-2  (#cbseNotes)

No comments:

Post a Comment

We love to hear your thoughts about this post!

Note: only a member of this blog may post a comment.