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

    Program to accept and display data for 1 object using pointer to object.

    Are you looking for a detailed solution to a query regarding a program to accept and display data for 1 object using a pointer to object? Follow this thread to get an answer to the query from our experts in the field.

    Following is a question that was asked in the MSBTE (Maharashtra State Board of Technical Education, Mumbai) Diploma in Computer Engineering question paper for Winter 2018 examination.

    This question is asked for 04 marks and hence requires some explanation along with the program code. Please let me know the solution.

    Q) Implement a program to declare a class city with data member's city name and state. Accept and display data for 1 object using a pointer to object.
  • Answers

    1 Answers found.
  • Class is a user defined data type but it behaves like built in data type. This is the real appeal of object oriented programming. So that we can easily produce object of required class as well as pointer of that class. The ease with which we can store address of integer to integer pointer, with the same ease we can also store address of object of a class to class pointer. When we have a object pointer and if we want to access any member of the class(most of the times functions available in public visibility of the class)then we have to use arrow(->) operator along with pointer variable. This concept is used in the program given below.

    Please add iostream.h and conio.h header files before declaring the class

    class city
    char cityname[20],state[20];
    void input(void)
    cout<<"Enter name of city and name of state\n";
    void display(void)
    cout<<"\n"< }
    void main(void)
    class city c, *p;

    In the above program, we declare two variables one is object of class city which is 'c' and another is pointer variable of type class city which is '* p'.
    As we know that pointer is a variable which is able to hold the address of another appropriate variable. Here also pointer p hold address of object c. Once we have pointer to object and if we want to access members of class using that pointer, we have to use arrow(->) operator. So that we have given function calls like p->input() and

  • Sign In to post your comments