Computer Science With C++ Chapter 4 Constructor And Destructor
  • Sponsor Area

    NCERT Solution For Class 12 Computer And Communication Technology Computer Science With C++

    Constructor And Destructor Here is the CBSE Computer And Communication Technology Chapter 4 for Class 12 students. Summary and detailed explanation of the lesson, including the definitions of difficult words. All of the exercises and questions and answers from the lesson's back end have been completed. NCERT Solutions for Class 12 Computer And Communication Technology Constructor And Destructor Chapter 4 NCERT Solutions for Class 12 Computer And Communication Technology Constructor And Destructor Chapter 4 The following is a summary in Hindi and English for the academic year 2021-2022. You can save these solutions to your computer or use the Class 12 Computer And Communication Technology.

    Question 1
    CBSEENCO12011613

    Differentiate between Constructor and Destructor functions giving suitable example using a class in C++. When does each of them execute?

    Solution
     

    Constructor

    Destructor

    Purpose

    Constructor is used to initialize the instance of a class.

    Destructor destroys the objects when they are no longer needed.

    When Called

    Constructor is Called when new instance of a class is created.

    Destructor is called when instance of a class is deleted or released.

    Memory Management

    Constructor allocates the memory.

    Destructor releases the memory.

    Arguments

    Constructors can have arguments.

    Destructor can not have any arguments.

    Overloading

    Overloading of constructor is possible.

    Overloading of Destructor is not possible.

    Name

    Constructor has the same name as class name.

    Destructor also has the same name as class name but with (~) tiled operator.

    Syntex

    ClassName(Arguments)
    {
    ...Body of Constructor
    }

    ~ ClassName()
    {
    }

    Example:
    Class Exam
    {
    int Eno; float Marks;
    public:
    Exam() ...Constructor
    {
    Eno=1; Marks = 100;
    cout<<'Constructor executed...'<<endl;
    }
    void Show()
    {
    cout<<Eno<<'#'<<Marks<<endl;
    }
    ~Exam() ...Destructor
    {
    cout<<'Exam Over'<<endl;
    }

    void main()
    {
    Exam E; ...Executes constructor
    E.Show();
    } ...Executes Destructor
    Question 3
    CBSEENCO12011665

    What is a copy constructor ? Give a suitable example in C++ to illustrate with its definition within a class and a declaration of an object with the help of it.

    Solution

    A copy constructor is an overloaded constructor in which an object of the same class is passed as a reference parameter.

    class Point
    {
    	int x;
    public:
    	Point(){
    		x=0;
    	}
    	Point(Point & p) // Copy constructor
    	{
    		x = p.x;
    	}
    };
    void main()
    {
    	Point p1;
    	Point p2(p1);//Copy constructor is called here
    }

    Question 6
    CBSEENCO12011668

    Write the definition of a class PIC in C++ with the following description :

    Private Members
    – Pno            //Data member for Picture Number (an integer)
    – Category      //Data member for Picture Category (a string)
    – Location     //Data member for Exhibition Location (a string)
    – FixLocation //A member function to assign
                 //Exhibition Location as per category
                //as shown in the following table
    Category Location
    Classic Amina
    Modern Jim Plaq
    Antique Ustad Khan
    Public Members
    – Enter()    //A function to allow user to enter values
                //Pno, category and call FixLocation() function
    – SeeAll() //A function to display all the data members

    Solution
    class PIC
    {
    	int Pno;
    	char Category[20];
    	char Location[20];
    	void FixLocation();
    public:
    	void Enter();
    	void SeeAll();
    };
    void PIC::FixLocation()
    {
    	if(strcmpi(Category,'Classic')==0){
    		strcpy(Location,'Amina');
    	}
    	else if(strcmpi(Category,'Modern')==0){
    		strcpy(Location,'Jim Plaq');
    	}
    	else if (strcmpi(Category,'Antique')==0)
    	{
    		strcpy(Location,'Ustad Khan');
    	}
    }
    void PIC::Enter()
    {
    	cin>>Pno;gets(Category);
    	FixLocation();
    }
    void PIC:: SeeAll()
    {
    	cout<<Pno<<Category<<Location<<endl;
    }

    Mock Test Series

    Sponsor Area

    Sponsor Area

    NCERT Book Store

    NCERT Sample Papers

    Entrance Exams Preparation

    22