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.

2 Answers

Answer :

#include<iostream.h>

#include<conio.h>

#include<string.h>

class student

{

int roll_no;

char name[20],department[40];

public:

student(int rno,char *n,char *d="CO")

{

roll_no=rno;

strcpy(name,n);

strcpy(department,d);

}

void display()

{

cout<<"\n Roll No:"<<roll_no;

cout<<"\n Name:"<<name;

cout<<"\n Department:"<<department;

}

};

void main()

{

student s1(112," Chitrakshi"),s2(114,"Anjali");

clrscr();

s1.display();

s2.display();

getch();

}


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

Related questions

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 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 : 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 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 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 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 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 create a class STUDENT The data members of STUDENT class. Roll_No Name Marks

Last Answer : #include #include class STUDENT { int Roll_No; char Name[20]; float Marks; }; OR #include #include class STUDENT { int Roll_No; char Name[20]; float Marks; public: void Accept(); void Display(); }; void STUDENT::Accept() { cout

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 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 : Accept data for five students and display it. Write a C++ program to displya sum of array elements of array size n. 

Last Answer : #include<iostream.h> #include<conio.h> void main() { int arr[20],i,n,sum=0; clrscr(); cout<<"\nEnter size of an array:"; cin>>n; cout<<"\nEnter ... ; } cout<<"\nSum of array elements is:"<<sum; getch(); }

Description : Describe with examples, passing parameters to base class constructor and derived class constructor by creating object of derived class.

Last Answer : When a class is declared, a constructor can be declared inside the class to initialize data members. When a base class contains a constructor with one or more arguments then it is mandatory for the derived class to have a ... #include class base { int x; public: base(int a) { x=a; cout

Description : Give output for following code: class student { int roll no; char name [14]; } s[6]; void main() { cout<<sizeof(s); }

Last Answer : Considering roll_no(Single variable) the output is: 96 OR Considering roll, no (Two variables) the output is: 108 OR Considering roll no the output is: error – space between roll and no

Description : Write a C++ program to write ‘Welcome to poly’ in a file. Then read the data from file and display it on screen.

Last Answer : #include #include #include void main()  { char str[25] = "Welcome to poly",ch; clrscr(); ofstream fout; fout.open("output.txt"); fout

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 C++ program to accept array of five elements, find and display smallest number from an array.

Last Answer : #include<iostream.h> #include<conio.h> void main() { int a[5],smallest,i; clrscr(); cout<<" Enter array elements:"; for(i=0;i<5;i++) cin>>a[i] ... } } cout<<endl<<"Smallest number="<<smallest; getch(); }

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

Last Answer : #include<iostream.h> #include<conio.h> class city { char city_name[20],state[20]; public: void accept() { cout<<"\nEnter city data:"; cout<<"\nName:"; ... ); ptr=&c; ptr->accept(); ptr->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 : Define a class circle having data members pi and radius. Initialize and display values of data members also calculate area of circle and display it.

Last Answer : class abc {  float pi,radius; abc(float p, float r) { pi=p; radius=r; } void area() { float ar=pi*radius*radius; System.out.println("Area="+ar); } void display() { System.out.println("Pi="+pi ... void main(String args[]) { abc a=new abc(3.14f,5.0f); a.display(); a.area(); } }

Description : Write a C++ program to append data from abc.txt to xyz.txt file.

Last Answer : Assuming input file as abc.txt with contents "World" and output file named as xyz.txt with contents "Hello" have been already created. #include #include int main() { fstream f; ifstream fin; fin.open("abc.txt",ios::in); ofstream fout; fout.open("xyz.txt", ios::app); if (!fin)  {  cout

Description : Define class and object. (OBJECT-ORIENTED-PROGRAMMING-WITH-C++)

Last Answer : Class: Class is a user defined data type that combines data and functions together. It is a collection of objects of similar type.  Object: It is a basic run time entity that represents a person, place or any item that the program has to handle.

Description : Write a program to implement multiple inheritance as shown in following Figure No.1: Accept and display data for one object of class result.  

Last Answer : Program: #include<iostream.h> #include<conio.h> class Subject1 { protected: float m1; }; class Subject2 { protected: float m2; }; class Result:public Subject1,public Subject2 { ... ; clrscr(); r.accept(); r.calculate(); r.display(); getch(); }

Description : Write a C++ program to find smallest number from two numbers using friend function. (Hint: use two classes). 

Last Answer : #include<iostream.h> #include<conio.h> class class2; class class1 { int no1; public: void get1() { cout<<"Enter number 1:"; cin>>no1; } friend void smallest( ... clrscr(); c1.get1(); c2.get2(); smallest(c1,c2); getch(); }

Description : Write a C++ program to overload binary operator ‘+’ to concatenate two strings.

Last Answer : #include<iostream.h> #include<conio.h> #include<string.h> class opov  { char str1[10]; public: void getdata() { cout<<"\nEnter a strings"; cin>>str1; } ... ,o2; clrscr(); o1.getdata(); o2.getdata();  o1+o2; getch();  }

Description : Write a C++ program to find greatest number among two numbers from two different classes using friend function.

Last Answer : include #include class second; class first { int x; public: void getx() { coutx; } friend void max(first,second); }; class second { int y; public: void gety() { couty; } friend void max(first,second); }; void max(first a,second b) { if(a.x>b.y) { cout

Description : Write a C++ program to swap two integer numbers and swap two float numbers using function overloading.

Last Answer : #include<iostream.h> #include<conio.h> void swap(int a,int b) { int temp; temp=a;  a=b;  b=temp; cout<<"\nInteger values after swapping are:"<<a<<" "< ... { clrscr(); swap(10,20); swap(10.15f,20.25f); getch(); }

Description : Write a C++ program to count number of spaces in text file.

Last Answer : Program: #include<iostream.h> #include<conio.h> #include<fstream.h> void main() { ifstream file; int s=0; char ch; clrscr(); file.open("abc.txt"); while ... cout<<"\nNumber of spaces in text file are:"<<s; getch(); }

Description : Write a ‘C++’ program to find factorial of given number using loop.

Last Answer : #include<iostream.h> #include<conio.h> void main() { int no,fact=1,i; clrscr(); cout<<"Enter number:"; cin>>no; for(i=1;i<=no;i++) { fact=fact*i; } cout<<"Factorial ="<<fact; getch(); }

Description : Write a C++ program for following multilevel inheritance.   Accept and display data for one car with all details.   

Last Answer : #include<iostream.h> #include<conio.h> class Carmanufacturer  { char Name[10]; public: void getcarm() { cout<<"\nEnter Car Name "; cin>>Name; } void putcarm() ... getcar(); c.putcarm(); c.putcarmodel(); c.putcar(); getch();  }

Description : Write a C++ program to implement following inheritance. Accept and display data for one programmer and one manager. Make display function virtual.  

Last Answer : #include #include class Employee  { int empid,empcode; public: void emp() { coutempid; coutempcode; } void virtual display() { cout

Description : Write a C++ program to find whether the entered number is even or odd.

Last Answer : #include<iostream.h> #include<conio.h> void main()  { int num; clrscr(); cout<<"\nEnter a Number "; cin>>num; if(num%2==0)  { cout<<"\nEntered ... else  { cout<<"\nEntered number is odd";  } getch();  }

Description : Write a C++ program to count number of spaces present in contents of file.

Last Answer : #include<iostream.h> #include<fstream.h> #include<conio.h> void main() { ifstream file; charch; int s=0; clrscr(); file.open("abc.txt"); while(file) { ... of spaces present in the content of the given file are:"<<s; getch(); }

Description : Write a C++ program to print multiplication table of 7. (example: 7 x 1 ….7 x 10 = 70) 

Last Answer : #include #include void main() { int num; clrscr(); cout

Description : Write a C++ program to implement inheritance shown in following figure: Accept and display data of one teacher and one student using object of class ‘Info’

Last Answer : #include<iostream.h> #include<conio.h> class Teacher { protected: char Name[20]; int empid; }; class Student { protected: char sname[20]; int rollno; }; class Info:public ... I.acceptT(); I.displayT(); I.acceptS(); I.displayS(); getch(); }

Description : Write syntax to define a derived class

Last Answer : Syntax: class derived_class_name : visibility_mode/access_specifier base_class_name { class body };

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 : 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 : Give a method to create, declare and initialize structure also develop a program to demonstrate nested structure.

Last Answer : Declaration of structure:- struct structure_name { data_type member 1; data_type member 2; . . . data_type member n; } structure variable 1, structure variable 2,..., structure variable n ... .collegeid); printf("\n College name=%s",s.c.collegename); getch(); }

Description : With suitable diagram describe structure of C++ program.

Last Answer : General C++ program has following structure. INCLUDE HEADER FILES CLASS DECLARATION MEMBER FUNCTIONS DEFINITIONS MAIN FUNCTION PROGRAM   Description:-  ... Function Program In this section programmer creates objects and calls various functions writer within various class. 

Description : Describe how memory is allocated to objects of class with suitable diagram.

Last Answer : Description: The memory space for object is allocated when they are declared and not when the class is specified. Actually, the member functions are created and placed in memory space only once when ... variables will hold different data values for different objects this is shown in fig:  

Description : Describe derived class with example.

Last Answer : Derived class: In inheritance a new class is derived from an old class. The new class is referred as derived class. The derived class can inherit all or some properties of its base class. Example: class base { }; class derived: public base { }; 

Description : Describe the concept of virtual base class with suitable example.

Last Answer : Virtual Base Class: An ancestor class is declared as virtual base class which is used to avoid duplication of inherited members inside child class due to multiple path of inheritance. Consider a hybrid inheritance as ... obj.getmarks(); obj.getscore(); obj.display(); getch(); }

Description : Describe use of protected access specifier used in the class.

Last Answer : Protected access specifier is use to declare a class member that is accessible by the member functions within its class and any class immediately derived from it.

Description : What is parameterized constructor?

Last Answer : A constructor that accepts parameters is called as parameterized constructor. In some applications, it may be necessary to initialize the various data members of different objects with different values when they are ... an argument. Member function put ( ) displays the value of data member m .

Description : Describe following terms: Inheritance, data abstraction, data encapsulation, dynamic binding.

Last Answer : Inheritance: 1. Inheritance is the process by which objects of one class acquire the properties of objects of another class. 2. It supports the concept of hierarchical classification. It also provides the ... with a given procedure call is not known until the time of the call at run-time.

Description : Describe use of static data member.

Last Answer : Use of static data member: Static data member (variable) is used to maintain values common to the entire class. Only one copy of static member is created for the entire class and is shared by all the objects of that class. Its lifetime is the entire program.

Description : Describe use of static data member in C++ with example.

Last Answer : Use of static data member: 1. Static data member is used to maintain values common to the entire class. 2. It is initialized to zero when the first object of its class is created. 3. Only one copy of that member is ... (); test t3; t3.getdata(); test::showcount(); getch(); }

Description : Write any two characteristics of destructor.

Last Answer : Characteristics: 1. It is used to destroy objects created by a constructor. 2. Name of destructor and name of the class is same. 3. Its name is preceded with tilde (~) symbol. 4. It ... the compiler upon exit from the program (or block or function) i.e when scope of object is over.

Description : Write the use of ios : : in and ios : : out.

Last Answer : ios::in - It is used as file opening mode to specify open file reading only.  ios::out- It is used as file opening mode to specify open file writing only.