Please disable adblock to view this page.

← Go home

Personal Database using types of constructors, friend, inline and dynamic memory concepts (C++)

April 23, 2016
Published By : Pratik Kataria
Categorised in: ,

/*
Develop an object oriented program in C++ to create a database of personal 
information system containing the following information: 1. Name 2. Date of Birth 
3. Blood Group 4. Height 5. Weight 6. Insurance Policy No. 7. Address 
8. Telephone Number 9. Driving lincense number. Construct the database with 
suitable member functions, for initializing and destroying the data using 
constructor, default constructor, copy constructor, destructor, 
static member functions, friend class, this pointer, inline code and dynamic 
memory allocation operators new and delete.
*/



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

using namespace std;

class Account;

class Person{

    char * name;
    char dob[20];
    char bloodg[20];
    int height;
    int weight;
    int IPNo;
    string addr;
    int ph_no;
    int drivinglicno;
    
    friend class Account;
    
public:
    static int count; //static automatically makes the initialization 0.
    
    Person();
    ~Person() { cout<<"\n Destruction has been called \n"; }
    inline void display();        //For inline stack is not created as it is usally done for a class.
    
    //Person(char *name, char dob[], char bloodg[], int, int, int, char addr[], int, int);
    Person(char *,char [], char [], int, int, int, string, int, int);
    Person(Person *p2);
    
    static void getcount(){
    cout<<count;        //since count being static is part of the class
    }

};

int Person::count = 0;

Person::Person(){

    name = new char();
    strcpy(dob, "00/00/0000");
    strcpy(bloodg, "");
    height = 0;
    weight = 0;
    IPNo = 0;
    addr = " ";
    ph_no = 0;
    drivinglicno = 0;
    count++;
}

Person::Person(char * n1, char db[20], char bg[20], int height, int weight, int IPNo, string addr,int ph_no, int drivinglicno){
    strcpy(this->name, n1);
    strcpy(this->dob, db);
    strcpy(bloodg, bg);
    this->height = height;
    this->weight = weight;
    this->IPNo = IPNo;
    this->addr = addr;
    this->ph_no = ph_no;
    this->drivinglicno = drivinglicno;
    count++;
}

Person::Person(Person *p2){ //It is *p1 and not &p1. Pointer since address is being passed.
    this->name = new char(); //[Important] This is needed.
    strcpy(this->name, p2->name);
    strcpy(this->dob, p2->dob);
    strcpy(bloodg, p2->bloodg);
    this->height = p2->height;
    this->weight = weight;
    this->IPNo = IPNo;
    this->addr = addr;
    this->ph_no = ph_no;
    this->drivinglicno = drivinglicno;
    count++;

}

inline void Person::display(){
    cout<<"Name : "<<name<<"\n";
    cout<<"Date of Birth : "<<dob<<"\n";
    cout<<"Blood Group : "<<bloodg<<"\n";
    cout<<"Height : "<<height<<"\n";
    cout<<"Weight : "<<weight<<"\n";
    cout<<"Insurance Policy No. : "<<IPNo<<"\n";
    cout<<"Address : "<<addr<<"\n";
    cout<<"Mobile Number : "<<ph_no<<"\n";
    cout<<"Driving License : "<<drivinglicno<<"\n";

}

class Account{
    int Accno;
    char Acc_Type[20];
public:
    
    Account(){
        Accno = 0;
        strcpy(Acc_Type,"Undefined");
    }
    
    void print(Person *p){
        cout<<"Account Number: "<<Accno<<endl;
        cout<<"Name: "<<p->name<<endl;
        cout<<"Account Type: "<<Acc_Type<<endl;
    }
};

int main(){
    Account obj;
    Person p1;
    char name[20];
    char dob[20];
    char bloodg[20];
    string addr;
    int weight, height, ipno, ph_no, dlc;
    
    cout<<"Welcome to personal database\n";
    
    cout<<"Enter Name:"<<endl;
    cin>>name;
    
    cout<<"Enter DOB (dd/mm/yyyy)"<<endl;
    cin>>dob;
    
    cout<<"Enter Blood Group:"<<endl;
    cin>>bloodg;
    
    cout<<"Enter Height (cm):"<<endl;
    cin>>height;
    
    cout<<"Enter Weight (kg):"<<endl;
    cin>>weight;
    
    cout<<"Enter Insurance Policy No.:"<<endl;
    cin>>ipno;
    
    cout<<"Enter Address:"<<endl;
    cin.ignore();
    getline(cin, addr);
    
    cout<<"Enter Mobile Number:"<<endl;
    cin>>ph_no;
    
    cout<<"Enter Driving License Number (Only digits):"<<endl;
    cin>>dlc;
    
    cout<<"———————————\n";
    cout<<"The details entered are: (Parameterized constructor)\n";
    Person p2(name,dob,bloodg,height, weight, ipno,addr,ph_no,dlc);
    
    p2.display();
    
    Person p3(&p2);
    cout<<"———————————\n";
    cout<<"The details entered are: (Copy constructor)\n";
    p3.display();
    
    cout<<"———————————\n";
    
    cout<<"Usage of friend class: \n";
    obj.print(&p2);
    
    cout<<"———————————\n";
    
    cout<<"The number of people registered in the database are : ";
    Person::getcount();
    cout<<"\n";
    
    cout<<"———————————\n";
    
    return 0;
}

View Article Page
Download

Pratik Kataria is a budding programmer, web designer and developer.