Implement a program to declare a class city with data members city name and state. Accept and display data for 1 object using pointer to object.

1 Answer

Answer :

#include<iostream.h>

#include<conio.h>

class city

{

char city_name[20],state[20];

public:

void accept()

{

cout<<"\nEnter city data:";

cout<<"\nName:";

cin>>city_name;

cout<<"\nState:";

cin>>state;

}

void display()

{

cout<<"\nCity data is:";

cout<<"\nName:"<<city_name;

cout<<"\nState:"<<state;

}

};

void main()

{

city c,*ptr;

clrscr();

ptr=&c;

ptr->accept();

ptr->display();

getch();

}


Related questions

Description : 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.

Last Answer : #include<iostream.h> #include<conio.h> #include<string.h> class student { char name[20]; float per; public: student(char n[],float p) { strcpy(name,n); per=p; } ... { student S("Sachin",78.00); clrscr(); S.putdata(); getch(); }

Description : Write a program to implement single inheritance. Declare base class employee with Emp_No. and Emp_Name. Declare derived class fitness with height and weight. Accept and display data for one employee.

Last Answer : #include<iostream.h> #include<conio.h> class employee { protected: int emp_no; char emp_name[10]; public: void gete() { cout<< enter employee details ; cin>>emp_no; cin>> ... f.gete(); f.pute(); f.getft(); f.putft(); getch(); }

Description : Write a program to declare a class 'staff' having data members as name and department. Accept this data for 10 staffs and display names of staff that are in 'CO' department.

Last Answer : #include<iostream.h> #include<conio.h> #include<string.h> class staff {  char name[10], dept[10];  public:  void accept() { cout<<"Enter Name and Department:\t"; cin>> ... i=0;i<=10;i++) { s[i].display(); } getch(); }

Description : Write a program in C++ to declare a class „Journal‟ having data members as journal_nm, price, ISSN_No. Accept this data for two objects and display the name of the journal having greater price.

Last Answer : #include<iostream.h> #include<conio.h> class Journal { char journal_nm[20]; int ISSN_No; float price; public: void accept(); void display(Journal); }; void Journal::accept() { ... clrscr(); j1.accept(); j2.accept(); j1.display(j2); getch(); }

Description : Write a program to declare class having data member as hrs, mins, secs. Write constructor to assign values and destructor to destroy values. Accept & display data for one object.

Last Answer : #include<iostream.h> #include<conio.h> class time { private: int hrs, mins,sec; public: time(int h,int m,int s) { hrs=h; mins=m; sec=s; } ~time() { cout<< hours ... } }; void main() { time t(2,43,56); t.display(); getch(); }

Description : Write a program to declare a class ‘book’ containing data members as ‘title’, ‘author-name’, ‘publication’, ‘price’. Accept and display the information for one object using pointer to that object.

Last Answer : #include<iostream.h> #include<conio.h> class book { char author_name[20]; char title[20]; char publication[20]; float price; public: void Accept(); void Display(); }; void ... title \t author_name \t publication \t price\n ; p-> Display(); getch(); }

Description : Write a program to implement the concept of virtual base class for following figure. Accept and display information of one employee with his name, code, basic pay, experience and gross salary with the object of employee class.

Last Answer : #include<iostream.h> #include<conio.h> class Master  {  char name[10],code[3];  public:  void acceptM() { cout<<"\nEnter name and code "; cin>>name>>code; ... .displayM(); e.displayA(); e.displayD(); e.displayE(); getch();  }

Description : Write a program in C++ to declare a class measure having data members as add 1, add 2, add 3. Initialize the values of two data members using constructor and display their addition using function.

Last Answer : #include<iostream.h> #include<conio.h> class measure { public: int add1,add2,add3; measure(int a,int b) { add1=a; add2=b; } void cal() { add3=add1+add2; } void display() ... ;>a>>b; measure m1(a, b); m1.cal(); m1.display(); getch(); }

Description : Write a program in C++ to accept a string from a user and display its reverse using pointer.

Last Answer : #include<iostream.h> #include<conio.h> #include<string.h> void main() { char str[20],*ptr; int l; clrscr(); cout<<"\n Enter a string : "; cin>>str; l=strlen(str); ... (l!=0) { ptr--; cout<<*ptr; l--; } getch(); }

Description : Implement single inheritance for following fig. Accept and display data for 1 table.

Last Answer : #include class furniture { protected: char material[20]; int price; }; class table :public furniture { int height ; float sur_area; public: void getdata() { coutmaterial; coutprice; coutheight; coutsur_area; } void putdata() { cout

Description : Write a C++ program to declare a class COLLEGE with members as college code. Derive a new class as STUDENT with members as studid. Accept and display details of student along with college for one object of student.

Last Answer : #include<iostream.h> #include<conio.h> class COLLEGE { protected: int collegecode; }; class STUDENT:public COLLEGE { int studid; public: void accept() { cout<<"Enter college ... STUDENT s; clrscr(); s.accept(); s.display(); getch(); }

Description : Write a C++ program to declare a class ‘Account’ with data members as accno, name and bal. Accept data for eight accounts and display details of accounts having balance less than 10,000.

Last Answer : #include<iostream.h> #include<conio.h> class Account  { long int accno, bal; char name[10]; public: void getdata() { cout<<"\nEnter account number, balance and name "; cin>> ... 0;i<8;i++)  { a[i].putdata();  } getch();  }

Description : Write a C++ program to declare a class ‘circle’ with data members as radius and area. Declare a function getdata to accept radius and putdata to calculate and display area of circle.

Last Answer : #include<iostream.h> #include<conio.h> class circle { float radius,area; public: void getdata() { cout<<"Enter radius:"; cin>>radius; } void putdata() { area=3 ... { circle c; clrscr(); c.getdata(); c.putdata(); getch(); }

Description : Write a C++ program to declare a class ‘College’ with data members as name and college code. Derive a new class ‘student’ from the class college with data members as sname and roll no. Accept and display details of one student with college data.

Last Answer : #include<iostream.h> #include<conio.h> class college { char name[10]; int collegecode; public: void getcollege() { cout<<"Enter college name:"; cin>>name; cout< ... (); s.getstudent(); s.putcollege(); s.putstudent(); getch(); }

Description : State and describe access specifiers used inside class to declare members.

Last Answer : Access specifiers: 1. private 2. protected 3. public Private access specifier: Class members declared as private can be accessed only from within the class. Outside class access is not allowed for private members of ... { private: int a; protected: int b; public: void display() { cout

Description : Write a C++ program to declare a structure employee with members as empid and empname. Accept and display data for one employee using structure variable. 

Last Answer : #include<iostream.h> #include<conio.h> struct employee  { int empid; char empname[10];  }; void main()  { employee e; clrscr(); cout<<"\nEnter employee id ... empid; cout<<"\nThe Employee Name is "<<e.empname; getch();  }

Description : Write a C++ program to declare a class student with members as roll no, name and department. Declare a parameterized constructor with default value for department as ‘CO’ to initialize members of object. Initialize and display data for two students.

Last Answer : write a C plus plus program to declare a class which accept and display student information such as roll number division and percentage use get data and put data with required parameters

Description : Write a program in C++ to implement following inheritance. Assume suitable data.

Last Answer : #include<iostream.h> #include<conio.h> class employee { int empid; char empname[20]; public: void accept() { cout<<"\n enter empid, empname:"<<endl; cin>>empid>> ... m.acc1(); m.dis1(); w.acc2(); w.dis2(); getch(); }

Description : Write a program which concate and reverse string by using pointer to string.

Last Answer : #include #include void main()  { char str1[20], str2[20], rev[20],*p1,*p2; int cnt=0; clrscr(); coutstr2;  p1=&str1[0];  p2=&str2[0]; while(*p1!='\0')  { p1++;  } while(*p2!='\0')  { *p1=*p2; p1++; p2++;  }  *p1='\0'; cout

Description : Write a program which perform arithmetic operation using pointer.

Last Answer : #include<iostream.h> #include<conio.h> void main() { int num[5]={56,75,22,18,90}; int ptr; int i; cout<< array elements are:: ; for(i=0;i<5;i++) ptr=num; cout<< ... 3; cout<< value of ptr+=3:: <<*ptr; cout<< \n ; getch(); }

Description : Write a program to copy content of one string to another string using pointer to string.

Last Answer : #include<iostream.h> #include<conio.h> void main() { char str1[10],str2[10],*p1,*p2; clrscr(); cout<<"\n Enter a String"; cin>>str1; p1=&str1[0]; p2= ... } *p2='\0'; cout<<"Copied String is "<<str2; getch(); }

Description : Write a program to find length of a string using pointer to string.

Last Answer : #include<iostream.h> #include<conio.h> void main() { char str1[10],*ptr; int len=0; cout<<"enter string:"; cin>>str1; ptr=&str1[0]; while(*ptr!='\0 ... +; } cout<<"\nThe Length of a string is"<<len; getch(); }

Description : Write a program to search a number from an array using pointer to array.

Last Answer : #include<iostream.h> #include<conio.h> void main() { int a[5],i,*a1,no,flag=1; clrscr(); a1=&a[0]; cout<<"\nEnter array elements :"<<endl; for(i=0;i<5 ... =0) { cout<<"\n\t Number is not present.... "; } getch(); }

Description : Write a program in C++ to search an element from an array using pointer.

Last Answer : #include<iostream.h> #include<conio.h> void main() { int a[10], n, i,*p, flag=0, x; clrscr(); cout<<"Enter no. of array elements \n"; cin>>n; cout<< ... } if(flag==0) cout<<x<<"is not found \n"; getch(); }

Description : Write a C++ program to declare a class addition with data members as x and y. Initialize values of x and y with constructor. Calculate addition and display it using function ‘display’.

Last Answer : #include<iostream.h> #include<conio.h> class addition { int x,y; public: addition(int,int); void display(); }; addition::addition (int x1,int y1) { x=x1; y=y1; } void addition: ... y); } void main() { addition a(3,4); a.display(); getch(); }

Description : Write a program to declare structure employee having data member name, age, street and city. Accept data for two employees and display it.

Last Answer : #include<stdio.h> #include<conio.h> struct employee { char name[10],street[10],city[10]; int age; }; void main() { int i; struct employee e[2]; clrscr(); for(i=0;i<2 ... =%s",e[i].street); printf("\n City=%s",e[i].city); } getch(); }

Description : Describe pointer to object with an example. 

Last Answer : When address of an object of a class is stored into the pointer variable of the same class type then it is pointer to object. This pointer can be used to access the data member and member functions of same class. ... (); // Invoking getdata()using pointer to object p1. ptr->display(); }

Description : Write a program which implement the concept of overloaded constructor.

Last Answer : #include #include class integer { int m,n; public: integer() { m=0; n=0; } //default constructor 1 integer(int a, int b) { m=a; n=b; } //Parameterized constructor 2 integer(integer &i) { m=i.m; n=i.n; } //copy constructor 3 }; void display() { cout

Description : Write a program in C++ to implement single inheritance from following figure:

Last Answer : #include<iostream.h> #include<conio.h> class product { protected: int prodid; char prodname[20]; }; class edible:public product { char flavour[20]; public: void accept() { cout<< ... for(i=0;i<5;i++) { e[i].display(); } getch(); }

Description : Explain the derived class access by pointer.

Last Answer : When base class and its derived class both contains same function name then the function in base class is declared as virtual using keyword virtual preceding its normal declaration. When a function is made virtual, C ... . Example: #include class Base { public: virtual void show( ) { cout

Description : Explain pointer to derived class with example.

Last Answer : Pointers can be used to point to the base class objects and objects of derived class. Pointers to objects of base class are compatible with pointers to objects of a derived class. Single pointer variable can be made to point ... { int a; public:  void get() { couta; }  void put() { cout

Description : State and describe use of pointer operator and address operator. Give one example of each. 

Last Answer : Pointer operator:- * It is used to declare a pointer variable. Also used as "value at" operator to read value stored inside the address pointed by pointer. Example: int *ptr; Address operator:-& ... address of a variable can be stored in pointer variable. Example: int a,*ptr; ptr=&a;

Description : State two advantages of pointer.

Last Answer : Advantages of pointer: Pointers save the memory. Pointers reduce the length and complexity of a program. Pointers allow passing of arrays and strings to functions more efficiently. Pointers make possible to return more than one value from the function. Pointers increase the processing speed.

Description : What is structure? How user declare structure? Give example.

Last Answer : Structure is a collection of different data types written under a common name. It is a user defined data type. To Define a structure following Syntax is use:- struct structure_name { data_type variable 1; data_type ... () {  Person p1; cout>p1.name; cout> p1.age; cout> p1.salary; cout

Description : Explain the concept of this pointer.

Last Answer : Concept of this pointer: C++ use a unique keyword called “this” to represent an object that invokes a member function. This unique pointer is automatically passed to a member function when it is invoked. “this” is a pointer that always points to the object for which the member function is called.

Description : Define pointer variable. Give its syntax.

Last Answer : Definition: Pointer is a variable that holds memory address of another variable of similar data type.  Syntax to declare pointer variable: data_type *pointer_variable;

Description : Explain pointer arithmetic with example.

Last Answer : C++ allows pointers to perform the following arithmetic operations: a. A pointer can be incremented (++) or decremented (--) b. Any integer can be added or subtracted from a pointer. c. One pointer can be subtracted ... -: 56 value of ptr +2: 22 value of ptr-1: 75 value of ptr+3: 90

Description : Explain the concept of pointer to derived classes. 

Last Answer : Pointer to derived class: Pointers can be used to point to the base class objects and objects of derived class. Pointers to objects of base class are type-compatible with pointers to objects of a derived ... of B,then any reference to that member by cptr will always access the base class member. 

Description : Write use of ‘This’ pointer.

Last Answer : this pointer is used to represent an object that invokes a member function. It points to the object for which the function is called. It is also used to access members of object inside function definition of called function

Description : Define pointer. Give syntax for declaration of pointer. 

Last Answer : Definition: Pointer is a variable that holds memory address of another variable of similar data type.  Syntax to declare pointer variable: data_type *pointer_variable;

Description : With suitable example, describe use of this pointer.

Last Answer : 1. C++ uses a unique keyword called this to represent an object that invokes a member function. 2. This unique pointer is automatically passed to a member function when it is invoked. 3. this is a pointer that always ... ; public: void setdata(int x) { this ->a=x; } void putdata() { cout

Description : Define pointer.

Last Answer : Pointer is a variable which holds an address of another variable of same data type.

Description : Write a program to show object as function argument. 

Last Answer : #include<iostream.h> #include<conio.h> #include<string.h> class objarg  { char str[10]; public: void get() { cout<<"\n Enter a Message"; cin>>str; } void ... clrscr(); o1.get(); o2.copy(o1); o2.display(); getch();  }

Description : Explain object as a function argument using following points with suitable example: (i) Pass by value (ii) Pass by reference

Last Answer : (i) Pass by Value: When an object is passed by value to a function, a copy of that object is created and changes are reflected on the copy object not on original object. Example: #include #include class Example { int x; public: Example(int a)  {  x=a;  }  void print()  { cout

Description : State characteristic of static data member. Explain why static data member must be defined outside the class. 

Last Answer : Characteristics of static data members: 1. It is initialized to zero when the first object of its class is created. No other initialization is permitted. 2. Only one copy of that member is created ... member(s) it is necessary to make static members global and re-declared outside of the class.

Description : Define a class student with int id and string name as data members and a method void SetData ( ). Accept and display the data for five students.

Last Answer : import java.io.*; class student { int id; String name; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); void SetData() { try { System.out.println("enter id and name for student"); ... (String are[]) {  student[] arr;  arr = new student[5];  int i;  for(i=0;i

Description : Define the following terms: (i) Data abstraction (ii) Class (iii) Dynamic binding (iv) Polymorphism

Last Answer : (i) Data abstraction: Abstraction refers to the act of representing essential features without including the background details or explanation. Data abstraction is the process of defining a data type, often ... more than one form at different instances depending on the type or number of arguments. 

Description : Write a program to swap two integer values by using call by reference.

Last Answer : #include #include void swap(int *a,int *b) { int c; c=*a; *a=*b; *b=c; } void main() { int a,b; couta; coutb; cout

Description : Write a program to calculate area of circle and area of rectangle using function overloading. 

Last Answer : #include<iostream.h> #include<conio.h> float area(float a) { return (3.14*a*a); } int area(int p,int q) { return(p*q); } void main() { clrscr(); cout< ... ;<<area(6); cout<<"Area of Rectangle:"<<area(5,6); getch(); }

Description : Write a program using concept of pointers to string for performing following operations: (i) String concatenation (ii) String comparisons

Last Answer : (i) Program to implement String Concatenation: #include #include void main() { char s1[50],s2[30],*p,*q; clrscr(); couts1>>s2; p=s1; q=s2; while(*p!=NULL) { p++; } while(*q!=NULL) { *p=*q; p++; q++; } *p='\0'; cout