Recent questions tagged object-oriented-programming-with-c

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 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 : 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 : State and describe visibility modes and its effects used in inheritance.

Last Answer : Different visibility modes are: 1. Private 2. Protected 3. Public Effects of visibility modes in inheritance:  Private members of base class are not inherited directly in any ... members of derived class and public members of base class become public members of derived class.

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 : 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 : 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 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 : 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 : 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 : Differentiate between run time and compile time polymorphism.

Last Answer : Sr. No. Compile time polymorphism Runtime polymorphism 1 In this polymorphism, an object is bound to its function call at compile time. In this polymorphism, ... is implemented with operator overloading or function overloading It is implemented with virtual function.

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 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 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 : Give meaning of following statements: int *ptr, a = 5; ptr = & a; cout

Last Answer : Declare pointer variable ptr and variable a with initial value 5. ptr = & a; initialize pointer variable with address of variable a (store address of variable a in ptr) cout

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

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 : State use of scope resolution operator.

Last Answer : It is used to uncover a hidden variable. Scope resolution operator allows access to the global version of a variable. The scope resolution operator is used to refer variable of class anywhere in program ... to define function outside of class.  Return_typeclass_name:: function_name( )  {  }  

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 : State the use of cin and cout.

Last Answer : cin: cin is used to accept input data from user (Keyboard). cout:cout is used to display output data on screen.

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 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 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 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 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 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 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 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 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 : 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 : 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 : 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 : 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 : With suitable example, describe effect of ++ and - - operators used with pointer in pointer arithmetic. 

Last Answer : ++ Operator: - It is referred as increment operator that increments the value of variable. If ++ operator is used with pointer variable, then pointer variable points to next memory address that means pointer increment with respect to size ... int a[5]={10,20,30,40,50},*ptr; ptr=a[0]; for(i=0;i

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 : 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 : Write syntax to define a derived class

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

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 : Describe meaning of the following (i) ios : : in (ii) ios : : out

Last Answer : (i) ios : : in : It is a file mode. It is used to open a file in read only mode.  (ii) ios : : out : It is a file mode. It is used to open a file in write only mode. 

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 : 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 : State the difference between OOP and POP.

Last Answer : OBJECT ORIENTED PROGRAMMING (OOP) PROCEDURE ORIENTED PROGRAMMING (POP) Focus is on data rather than procedure. Focus is on doing things (procedure). Programs are divided into multiple ... approach is used in C++ language. Procedure oriented approach is used in C language.

To see more, click for the full list of questions or popular tags.