You must Sign In to post a response.
  • Category: Programming

    Accept data for one student using constructor

    Having a query about a question pertaining to classes and constructors of diploma MSBTE exam? Searching for a solution to this question? Check out this page and get answers to your question.

    Following is the question asked in MSBTE(Maharashtra State Board of Technical Education, Mumbai) Diploma in Computer Engineering question paper for Summer 2018 examination. This question is asked for 4 marks and hence requires some explanation along with program code. Please let me know the solution. The question is as below
    Write a program to declare class student having data members name and percentage. Write constructor to initialize these data members. Accept and display this data for one object. [4 Mark]
  • Answers

    1 Answers found.
  • Object Oriented Programming has given us a very dynamic concept which is known as constructor. Constructor is a special member function of the class. One of his special thing is that the name of the constructor function is same as that of the class. Main task of constructor is to initialize the members of the object whenever object gets created. So it is a dynamic process of initialization of an object. There are different types of constructors, out of which constructor with parameters is used in the below program.
    Please include "iostream.h", "conio.h" and "string.h" header files in the program before definition of the class student.

    class student
    char * name;
    float percent;
    student(char * s, float p)
    cout<<"\nStudent details are as follows\n";
    cout<<"\nName of student is "< cout<<"\nPercentage of student is "< }
    void main(void)
    char * str;
    float perc;
    cout<<"\nEnter name of the student\n";
    cout<<"\nEnter percentage of student\n";
    class student s1(str,perc);

    In above program firstly name and percentage of student is accepted from user in main function and then object of class is created. While creating object of the class these two parameters namely name and percentage are supplied to the constructor function.
    In constructor function string is received in the character pointer and percentage is received in the float variable. To assign the value in character pointer we use strcpy() function and assignment operator for assigning the float value. As we use strcpy(), so for its smooth execution we must include string.h header file.

  • Sign In to post your comments