/*
Problem statement:
Develop an object oriented program in C++ to create a database of student information
system containing the following information: Name, Roll number, Class, division, Date of
Birth, Blood group, Contact address, telephone number, driving license no. etc Construct the
database with suitable member functions for initializing and destroying the data viz
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>			//header file used for cin and cout
#include<string.h>			//header file for string class
using namespace std;			//refer cin and cout
#define max 100;
class per_info								//stud_info is friend class of per_info
{
    string lic, dob, bldgrp;    //personal info variables
    public:
    per_info();                 //DECLARE DEFAULT CONSTRUCTOR//
    per_info(per_info &);       //DECLARE COPY CONSTRUCTOR//
    ~per_info()                 //DEFINITION AND DECLARATION OF DESTRUCTOR
    {
          cout<<"\nDESTRUCTOR IS CALLED!!!!!"<<endl<<"RECORD DELETED SUCCESSFULLY"<<endl;
    }
    friend class student;                     //FRIEND FUNCTION DECLARATION
};
class student   							//DEFINITION OF STUDENT CLASS
{
    string name, address, year;				//OBJECTS OF STRING CLASS
    char insurance_policy;
    float height,weight;
    long mob;
    static int cnt; 						//    STATIC VARIABLE DECLARATION
    public:
    void create(per_info &);				//TO CREATE DATABASE AND PASSED REFERENCE OF PERSONAL INFO OBJECT
    void display(per_info &);			//TO DISPLAY DATABASE
 
    inline static void inccnt()                //STATIC FUNCTION//
 
    {
      cnt++;
    }
    inline static void showcnt()               //STATIC FUNCTION//
                                      //INLINE FUNCTION//
    {
      cout<<"\nTOTAL NO OF RECORDS ARE : "<<cnt;
    }
    student();                           //DEFAULT CONSTRUCTOR//
    student(student &);                  //COPY CONSTRUCTOR OF STUDENT CLASS//
    ~student()                          //DESTRUCTOR OF STUDENT CLASS//
    {
       cout<<"\nDESTRUCTOR IS CALLED!!!"<<endl<<"RECORD DELETED SUCCESSFULLY"<<endl;
    }
};
 
int student::cnt;              //DEFINITION OF STATIC MEMBER//
student::student()                //CONSTRUCTOR DEFINITION//
{
        name="ANAGHA NIRGUDE";
        address="SR NO.81 BABBAR SOLANKI \nDIGHI, PUNE";
        year="SE COMPUTER";
        insurance_policy='y';
        height=3.2;
        weight=60.5;
        mob=942329999;
}
per_info::per_info()             //CONSTRUCTOR DEFINITION//
{
       lic="ABD45656";
       dob="02/11/1997";
       bldgrp="A-";
}
student::student(student &obj)			//DEFINITION OF COPY CONTRUCTOR OF STUDENT CLASS
{
      this->name=obj.name;			//this is a pointer points to the object which invokes it
      this->address=obj.address;	//this-> can be written as name
      this->year=obj.year;
      this->insurance_policy=obj.insurance_policy;
      this->height=obj.height;
      this->weight=obj.weight;
      this->mob=obj.mob;
}
per_info::per_info(per_info &obj)		//DEFINITION OF COPY CONTRUCTOR OF PERSONAL CLASS
{
     lic=obj.lic;
     dob=obj.dob;
     bldgrp=obj.bldgrp;
}
//TO CREATE THE DATABASE
//DEFINTION OF CREATE FUNTION
void student::create(per_info &obj)
{
cout<<"\nNAME : "<<endl;
cin>>name;
cout<<"\nADDRESS : "<<endl;
cin>>address;
cout<<"\nDATE OF BIRTH : "<<endl;
cin>>obj.dob;
cout<<"\nYEAR : "<<endl;
cin>>year;
cout<<"\ninsurance_policy: "<<endl;
cin>>insurance_policy;
cout<<"\nheight : "<<endl;
cin>>height;
cout<<"\nweight : "<<endl;
cin>>weight;
cout<<"\nBLOOD GROUP : "<<endl;
cin>>obj.bldgrp;
cout<<"\nLICEINCE NUMBER : "<<endl;
cin>>obj.lic;
cout<<"\nPHONE NUMBER : "<<endl;
cin>>mob;
}
 
//DEFINTION OF DISPLAY FUNCTION
//TO DISPLAY DATABASE
void student::display(per_info &obj)
{
cout<<"\n***********************"<<endl;
cout<<"\nNAME OF STUDENT : "<<name<<endl;
cout<<"\nADDRESS OF STUDENT : "<<address<<endl;
cout<<"\nDATE OF BIRTH : "<<obj.dob<<endl;
cout<<"\nYEAR : "<<year<<endl;
cout<<"\ninsurance_policy : "<<insurance_policy<<endl;
cout<<"\nheight : "<<height<<endl;
cout<<"\nweight : "<<weight<<endl;
cout<<"\nBLOOD GROUP : "<<obj.bldgrp<<endl;
cout<<"\nLICEINCE NUMBER : "<<obj.lic<<endl;
cout<<"\nPHONE NUMBER : "<<mob<<endl;
cout<<"\n***********************"<<endl;
}
int main()
{
 int n;				//COUNT OF NUMBER OF STUDENTS
 int ch;
 char ans;
 
 cout<<"\nENTER NO OF STUDENTS :"<<endl;
 cin>>n;
 cout<<"\n***********************"<<endl;
 student *sobj=new student[n];				//NEW IS DYNAMIC MEMORY ALLOCATION OPERATOR, sobj IS POINTER OF TYPE STUDENT AND IT IS A ARRAY OF OBJECT OF SIZE n
 per_info *pobj=new per_info[n];		//NEW IS DYNAMIC MEMORY ALLOCATION OPERATOR, pobj IS POINTER OF TYPE STUDENT AND IT IS A ARRAY OF OBJECT OF SIZE n
 
 do
 {
	 cout<<"\n Menu \n 1. Create Database \n 2. Display Databse \n 3. Copy Constructor\n 4. Default Constructor \n 5. Delete (Destructor)";
	 cout<<"\n Enter your Choice: ";
	 cin>>ch;
	 switch(ch)
	 {
	 case 1: // create database
	 {
		 for(int i=0;i<n;i++)
		 {     sobj[i].create(pobj[i]);
		       sobj[i].inccnt();}
	 }
		break;
 	 case 2: // Display Database
  		{
			sobj[0].showcnt(); 	//to display the total count of students
			//we can use any object of student class as it is static member function
			 for(int i=0;i<n;i++)
			 {
				 sobj[i].display(pobj[i]);
			 }
    	 }
    	 break;
	  case 3: // Copy Constructor
		{
			 student obj1;
			 per_info obj2;
			 obj1.create(obj2);
			 student obj3(obj1);		//invoking copy constructor of student info to copy contents from object 1 to 3
 
			 per_info obj4(obj2);		 //invoking copy constructor of personal info to copy contents from object 2 to 4
			 cout<<"\n Copy Constructor is called ";
			 obj3.display(obj4);
 		}
        break;
	 case 4: // Default Constructor
 		{
			 student obj1;	//obj1 is invoking default constructor of class student
			 per_info obj2;	//obj2 is invoking default constructor of class personal
			 cout<<"\n Default Constructor is called ";
			 obj1.display(obj2);
 		}
 		break;
	 case 5: // destructor is called
 
			 delete [] sobj;			//invoking destructor and delete sobj dynamically
 		     delete [] pobj;		//invoking destructor and delete pobj dynamically
 	}				//end of switch case
 	cout<<"\n Want to continue:(y/n)"	     ;
 	cin>>ans;
  }while(ans=='y') 	;
 
 
 return 0;
} 

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}