C++ Programming Language


---------------×------------------××--------------------×---------------

C++ Programming Language 

---------------×------------------××--------------------×---------------

Questions and Answers 

---------------×------------------××--------------------×---------------

1. Write a C++ Program to demonstrate constructor in derived class with respect to order of calling constructor and passing parameters to base class constructor.

Ans.:- 

#include<iostream.h>
#include<conio.h>
class base
{
      
      public: 
        base(int a) 
        {
         cout<<"This is base constructor a = "<<a;
        }
};
class sub: public base
{
        public :
          sub(int b): base(b)
          {
            cout << "This is sub class constructor b=" <<b;
           }
};
void main()
{
clrscr();
sub s(5);
getch();
}

Output :-

This is base class constructor a=5
This is sub class constructor b=5

---------------×------------------××--------------------×---------------

2. Compare Static and Non static data members.

Ans.:- 


---------------×------------------××--------------------×---------------

3. What is a class? Give its example.

Ans.:- 

Class is a user defined data type that combines data and functions
together. It is a collection of objects of similar type.
Example:
class Student
{
int rollno;
char name[10];
public:
void getdata( );
void putdata( );
};

---------------×------------------××--------------------×---------------

4. Write two properties of static member function.

Ans.:- 

i) A static member function can have access to only other static data
members and functions declared in the same class.
ii) A static member function can be called using the class name with a
scope resolution operator instead of object name as follows:
class_name::function_name;

---------------×------------------××--------------------×---------------

5. Write the applications of object oriented programming.

Ans.:- 

Applications of object oriented programming are:
1) Real time systems
2) Simulation and modeling
3) Object-oriented databases
4) Hypertext, hypermedia
5) AI and expert systems
6) Neural networks and parallel programming
7) Decision support and office automation systems
8) CIM/CAM/CAD systems

---------------×------------------××--------------------×---------------

6. Describe derived class with example.

Ans.:- 

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
{
};

---------------×------------------××--------------------×---------------

7. Write a C++ program to find smallest number from two numbers using friend function.

Ans.:- 

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

---------------×------------------××--------------------×---------------

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

Ans.:- 

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 constructor and pass arguments to the base class constructor. When both the derived and base classes contain constructors, the base constructor is executed first and then the constructor in the derived class is executed. The constructor of derived class receives the entire list of values as its arguments and passes them on to the base constructors in the order in which they are declared in the derived class.

General form to declare derived class constructor:

Derived-constructor (arglist1, arglist (D)):Base1(arglist1)
{
Body of derived class constructor
}

Derived constructor declaration contains two parts separated with colon (:). First part provides declaration of arguments that are passed to the derived constructor and second part lists the function calls to the base constructors.

Example:

#include<iostream.h>
#include<conio.h>
class base
{
int x;
public:
base(int a)
{
x=a;
cout<<"Constructor in base x="<<x;
}
};
class derived: public base
{
int y;
public:
derived(int a,int b):base(a)
{
y=b;
cout<<"Constructor in derived.y="<<y;
}
};
void main()
{
clrscr();
derived ob(2,3);
getch();
}
In the above example, base class constructor requires one argument and derived class constructor requires one argument. Derived class constructor accepts two values and passes one value to base class constructor.

---------------×------------------××--------------------×---------------

9. Write a program to swap two integers using call by reference method.

Ans.:- 

#include<iostream.h>
#include<conio.h>
void swap(int*p, int*q)
{
int t;
t=*p;
*p=*q;
*q=t;
}
void main()
{
int a,b;
float x,y;
clrscr();
cout<<"Enter values of a and b\n";
cin>>a>>b;
cout<<"Before swapping\n";
cout<<"a="<<a<<"\tb="<<b<<endl;
swap(&a, &b);
cout<<"After swapping\n";
cout<<"a="<<a<<"\tb="<<b<<endl;
getch();
}

---------------×------------------××--------------------×---------------

10. Properties of friend function.

Ans.:- Not in the scope of the class.

Since it is not in the scope of the class, it cannot be called from the object of that class, for example, sumComplex() is invalid.

A friend function can be invoked without the help of any object.

Usually contain objects as arguments
Can be declared under the public or private access modifier, it will not make any difference.

It cannot access the members directly by their names, it needs syntax (object_name.member_name) to access any member.

---------------×------------------××--------------------×---------------

11. List different types of inheritance .

Ans.:- 
  1. Single Inheritance 
  2. Multiple Inheritance 
  3. Multilevel Inheritance 
  4. Hierarchical Inheritance 
  5. Hybrid Inheritance 
---------------×------------------××--------------------×---------------

12. Single Inheritance 

Ans.:- A Single inheritance is one, when there exist a single base class and from it exactly one class is derived.

Syntax:-
 class base
 {
 …
 …
 };
 class derive : access_specifier base
 {
 ….
 ….
 };

---------------×------------------××--------------------×---------------

13. Multi-level inheritance

Ans.:- 
  • A Multilevel inheritance is one, when there exist a single base class and from it one ass is derived. This derived class act as a base class to other derived class.
  • The class can be derived till any level with condition that only one class has to be derived from each class. All base class from level 2 are known as intermediate class.
Syntax:-
 class base
 {
 …
 ...
 };
class base1 : access_specifier base
 {
 ….
 ….
 };
 class base2 : access_specifier base1
 {
 ….
 ….
 };
class derive : access_specifier base2
 {
 ….
 ….
 };

---------------×------------------××--------------------×---------------

14. Multiple Inheritance

Ans.:- It an inheritance type where more than one class give its properties to single class. There can be any number of classes properties can be reuse in one class.

Syntax:-
class base1
{
};
class base2
{
 …
 …
 };
class derive : access_specifier base1,
access_specifier base2, …access_specifier base_n
{
… 
};

---------------×------------------××--------------------×---------------

15. Hierarchical inheritance

Ans.:- In this inheritance, multiple classes can be derived from one single base class. All derived classes inherit properties of single base class.

Syntax:-
 class base
 {
 …
 …
 };
 class derive : access_specifier base
 {
 …
 …
 };
 class derive1 : access_specifier base
 {
 …
 …
 };

---------------×------------------××--------------------×---------------

16. Hybrid Inheritance

Ans.:-
  • When more than one type of inheritance is use in same program then it forms are called hybrid inheritance.
  • In this inheritance, it combines single inheritance, multiple inheritance, multi – level inheritance & hierarchical inheritance.
  • There is no fix structure available with hybrid inheritance. We can use almost any combination of inheritance type to form hybrid inheritance
---------------×------------------××--------------------×---------------

17. Characteristics of object oriented programming
Ans. 
1. Emphasis is on data rather than procedure.
2. Bottom up approach programming language.
3. Data is hidden.
4. More security for data compared to pop.
5. Larger programs are divided into objects.

---------------×------------------××--------------------×---------------

18. Characteristics of procedure oriented programming
Ans.
1. Emphasis is on procedure rather than data.
2. top down programing approach.
3. larger programs are divided in smaller programs.
4. data is not hidden in pop.
5.  data is shared openly every where from one function to another function.

---------------×------------------××--------------------×---------------

19. Applications of oop.
Ans. real time systems, simulations, object oriented databases, cam/cad systems, ai and expert systems, neural networks and parallel programming, etc.

---------------×------------------××--------------------×---------------

20. what are keywords ? list any 5 keywords in C++.
Ans. The keywords are special system defined words used in programming languages. 
1. static 
2. float
3. new
4. delete
5. char
6. int
7. long
8. double
9. friend
10. for

---------------×------------------××--------------------×---------------

21. What are object ?
Ans. Objects are basic run time entities. Object are created from classes.

---------------×------------------××--------------------×---------------

22. What is class ?
Ans. Class is collection of objects of similar type.

---------------×------------------××--------------------×---------------

23. What is polymorphism ?
Ans. Polymorphism means many forms. Ability to take more than one forms is known as polymorphism.

---------------×------------------××--------------------×---------------

24. Explain and State use of scope resolution operator with example.
Ans. Scope resolution operator is denoted by (::) pair of colons. Scope resolution operator is used to access global variable main() method. Scope resolution operator is also used for to write function definition outside the class. 
Example:-
#include<iostream.h>
#include<conio.h>
int a=10;
void main()
{
    int a=20;
    clrscr();
    cout<<"\nlocal a = "<<a;
    cout<<"\nGlobal a = "<<::a;
    getch();
}

---------------×------------------××--------------------×---------------

25. Memory management operator 
Ans. C++ provides new keyword for dynamic memory allocation and delete keyword for destroy or delete memory which is allocated by using new operator.

Syntax for new :-    datatype *ptr = new datatype;
Syntax for delete :-  delete ptr;

---------------×------------------××--------------------×---------------

26. Static data member 
Ans. We can create static data members as static using static keyword. It is initialized to zero when first object is created. only one copy of static variable is created for entire class. It is visible only within the class. Static variables are normally used to maintain values common to the entire class.

---------------×------------------××--------------------×---------------

27. What is constructor ?
Ans. Constructor is a special member function of class. Constructor is invoked when object of class is created. Both constructor name and class name are same. There is no need to call constructor. Constructor cannot have return type even void also. constructor is used to initialize the object of its class. 
There are Three types of constructor namely 
1. Default constructor 
2. Parameterized Constructor
3. Copy constructor

---------------×------------------××--------------------×---------------

28. Characteristics of Constructor
Ans. Constructor name and class name both are same. Constructor is invoked automatically when object of class is declared. They do not have return types. Constructor cannot be virtual. Constructor cannot be inherited. constructor is special member function of class. Constructor should be declared in public section.

---------------×------------------××--------------------×---------------

29. What is default Constructor ?
Ans. Default constructor is a constructor that do not accept any arguments. When constructor does not takes any arguments then it is called as default constructor. Default constructor has blank brackets.
Syntax :- 
class classname
{
        
        public:
            classname()     //default constructor
            {
                    //body of default constructor
            }
};

Note :- For example refer question no. 30
---------------×------------------××--------------------×---------------

30. Write a C++ Program declare a class measure having data members add1, add2, add3. Initialize the data members using constructor and store their addition in third data member using a function and display their addition.
Ans.
#include<iostream.h>
#include<conio.h>
class measure
{
    public:
    int add1,add2,add3;
    measure()
    {
    add1=10;
    add2=20;
    }
    void add()
    {
     add3=add1+add2;   
    }
    void display()
    {
      cout<<"Addition = "<<add3;
    }
};
void main()
{
    clrscr();
    measure m1;
    m1.add();
    m1.display();
    getch();
}

---------------×------------------××--------------------×---------------

31. Parameterized constructor 
Ans. Parameterized constructor is type of constructor which accepts some arguments. When constructor accepts some arguments is called as parameterized constructor.
Example :- 
#include<iostream.h>
#include<conio.h>
class circle
{
    public:
    float a,r;
    circle(float x)
    {
        r=x;
    }
    void compute();
    void display();
};
inline void circle::compute()
{
a=3.14*r*r;
}
inline void circle::display()
{
cout<<"\nArea of circle =  "<<a;
}
void main()
{
float p;
clrscr();
cout<<"\nEnter radius = ";
cin>>p;
circle c(p);
c.compute();
c.display();
getch();
}

---------------×------------------××--------------------×---------------

32. Copy constructor or multiple constructor
Ans. A constructor accepts a reference to its own class as a parameter is called as a copy constructor. When a constructor takes refence of object as a parameter then it is known as copy constructor. Copy constructor is also known as Multiple Constructor.
#include<iostream.h>
#include<conio.h>
class circle
{
    public:
    float a,r;
    circle(float x)
    {
        r=x;
    }
    circle(circle &c)
    {
        r=c.r;
    }
    void compute();
    void display();
};
inline void circle::compute()
{
a=3.14*r*r;
}
inline void circle::display()
{
cout<<"\nArea of circle =  "<<a;
}
void main()
{
float p;
clrscr();
cout<<"\nEnter radius = ";
cin>>p;
circle c(p);
c.compute();
c.display();
circle c1(c);
c1.compute();
c1.display();
getch();
}

---------------×------------------××--------------------×---------------

33. Rules for operator overloading
Ans.
1. Only existing operator can be overloaded. New operator cannot be created.
2. The overloaded operator must have at least one operand.
3. We cannot change basic meaning of an operator.
4. Overloaded follow operator overloading syntax rules.
5. Binary operator overloaded by means of member function can accept up to one parameter.
6. Binary operator overloaded by means of friend function can accept up to two parameters.
7. There are some operator that cannot be overloaded that are
  • sizeof()
  • membership operator (.)
  • pointer to member operator (.*)
  • scope resolution operator (::)
  • conditional operator (?:)

---------------×------------------××--------------------×---------------

34. Rules for virtual functions
Ans.
1. The virtual function cannot be static.
2. The virtual function can be accessed by using object pointer.
3. It can be friend of another class.
4. Virtual Constructor cannot be created.
5. Virtual Destructor can be created.
6. If virtual function is defined in the base class then it is not needed to redefine it in derived class.
7. The object pointer of base can point to both base class and derived class, but object pointer of derived class cannot point to base class.

---------------×------------------××--------------------×---------------

35. State and describe visibility modes and its effects used in inheritance.
Ans. 
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 visibility mode. 
1. Private visibility mode In this mode, protected and public members of base class become private members of derived class. 
2. Protected visibility mode In this mode, protected and public members of base class become protected members of derived class. 
3. Public visibility mode In this mode, protected members of base class become protected members of derived class and public members of base class become public members of derived class.

---------------×------------------××--------------------×---------------

36. Difference between Constructor and Destructor 
Ans.
Sr. No  Constructor Destructor
1.Constructor is used to create objects. Destructor is used to destroy objects.
2. It's name is name as class name. It's name is same as class name but preceded with tiled operator.
3. Constructor can take arguments. Destructor does not take any arguments.
4. Constructor is invoked when object of class is created. It is invoked by compiler when program reaches closing curly bracket.
5. Syntax:-
classname()
{
//body of constructor
}
Syntax:-
~classname()
{
//body of destructor
}

---------------×------------------××--------------------×---------------

37. Difference Between Compile Time Polymorphism and Run Time Polymorphism
Ans.
Sr. No  Compile Time Polymorphism Run Time Polymorphism
1. In compile time polymorphism call is resolved by compiler. In Run time polymorphism call is not resolved by compiler.
2. It is also known as static binding, early binding overloading as well. It is also known as late binding, dynamic binding and overriding as well.
3. Overloading is compile time polymorphism where more than one method share same name but with different parameters and different return type arguments. Overriding is run time polymorphism where one method as same name, same parameters but associated with different class.
4. It provides fast execution because it is known as early compile time. It provides slow execution compared to compile time polymorphism.
5. It is less flexible as all things execute at compile time. It is more flexible as all things execute at run time.
6. It is achieved by operator overloading and function overloading. It is achieved by virtual functions and pointers.

---------------×------------------××--------------------×---------------

38. Write a program in C++ to overload unary ‘-’ operator to negate values of data members of class.
Ans. 
#include<iostream.h> 
#include<conio.h> 
#include<string.h> 
class Number 
int x, y;  
public: 
Number (int a,int b) 
a =x; 
b =y; 
void display() 
cout<<"value of x=”<<x<<”\nValue of y= ”<<y; 
void operator - ( ) 
x = - x; 
y = - y; 
}; 
void main() 
Number N1(5,6); 
clrscr(); 
N1.display(); 
-N1; 
cout<<"\n After negation:"; 
N1. display (); 
getch(); 

---------------×------------------××--------------------×---------------

39. Write a C++ program to count number of spaces in text file.
Ans.
#include<iostream.h> 
#include<conio.h> 
#include<fstream.h> 
void main() 
ifstream file; 
int s=0; 
 char ch; 
clrscr(); 
file.open("abc.txt"); 
 while(file) 
 { 
file.get(ch); 
  if(ch==' ') 
  { 
   s++; 
  } 
 } 
cout<<"\nNumber of spaces in text file are:"<<s; 
getch(); 

---------------×------------------××--------------------×---------------

40. Write a C++ program to append data from abc.txt to xyz.txt file.
Ans.
Assuming input file as abc.txt with contents "World" and output file 
named as xyz.txt with contents "Hello" have been already created. 
   
#include <iostream.h> 
#include<fstream.h> 
 
int main() 
 fstream f; 
 ifstream fin; 
 fin.open("abc.txt",ios::in); 
 ofstream fout; 
 fout.open("xyz.txt",  ios::app); 
 if (!fin) 
           { 
     cout<< "file not found"; 
           } 
           else 
           { 
    fout<<fin.rdbuf(); 
           } 
         char ch; 
         f.seekg(0); 
         while (f) 
        {           
           f.get(ch); 
           cout<< ch; 
        } 
        f.close(); 
        return 0; 
}

---------------×------------------××--------------------×---------------

41.  Describe structure of C++ program with diagram. 
Ans.

Description:- 
1. Include header files 
In this section a programmer include all header files which are 
require to execute given program. The most important file is 
iostream.h header file. This file defines most of the C++statements 
like cout and cin. Without this file one cannot load C++ program. 
2. Declare Class 
In this section a programmer declares all classes which are necessary 
for given program. The programmer uses general syntax of creating 
class. 
3. Define Member Functions 
This section allows programmer to design member functions of a 
class. The programmer can have inside declaration of a function or 
outside declaration of a function. 
4. Define Main Functions 
This section the programmer creates object and call various functions 
writer within various class. 

---------------×------------------××--------------------×---------------

42. Describe following terms: Inheritance, data abstraction, data encapsulation, dynamic binding. 
Ans. 
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 idea of reusability. 

Data abstraction: 
1. Data abstraction refers to the act of representing essential features 
without including the background details or explanations. 
2. Classes use the concept of abstraction and are defined as a list of 
abstract attributes such as size, weight and cost and functions to 
operate on these attributes. 

Data encapsulation: 
1. The wrapping up of data and functions together into a single unit 
(called class) is known as encapsulation. 
2. By this attribute the data is not accessible to the outside world, 
and only those functions which are wrapped in the class can 
access it. 

Dynamic Binding: 
1. Dynamic binding refers to the linking of a procedure call to be 
executed in response to the call. 
2. It is also known as late binding. It means that the code associated 
with a given procedure call is not known until the time of the call 
at run-time. 

---------------×------------------××--------------------×---------------

43. Write the use of ios : : in and ios : : out.
Ans.
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. 

---------------×------------------××--------------------×---------------

44. Write a C++ program to write ‘Welcome to poly’ in a file. Then read the data from file and display it on screen.
Ans.
#include<iostream.h> 
#include<conio.h> 
#include<fstream.h> 
void main() 
  { 
char str[25] = "Welcome to poly",ch; 
clrscr(); 
ofstream fout; 
fout.open("output.txt"); 
fout<<str; 
 fout.close(); 
ifstream fin; 
fin.open("output.txt"); 
 while (!fin.eof())  
           {           
              fin.getline(str, 25);  
              cout<<str<<endl;  
          }  
fin.close(); 
getch(); 
  } 

---------------×------------------××--------------------×---------------

45. Write a C++ program to print multiplication table of 7.
Ans.
#include<iostream.h> 
#include<conio.h> 
void main() 
int num; 
clrscr(); 
cout<<"Multiplication table for 7 is:"<<endl; 
for(num=1;num<=10;num++) 
 { 
cout<<"7 *"<<num<<"="<<7*num<<endl; 
 } 
getch(); 
}

---------------×------------------××--------------------×---------------

46. Give syntax and use of fclose() function.
Ans. This function is used to close a file stream. The data that is buffered but not written is flushed to the OS and all unread buffered data is discarded.
Syntax :-    int fclose(FILE* stream);

---------------×------------------××--------------------×---------------

47. Write a program to swap two integers using call by reference method.
Ans.
#include<iostream.h> 
#include<conio.h> 
void swap(int*p, int*q) 
int t; 
 t=*p; 
 *p=*q; 
 *q=t; 
void main() 
int a,b; 
float x,y; 
clrscr(); 
cout<<"Enter values of a and b\n"; 
cin>>a>>b; 
cout<<"Before swapping\n"; 
cout<<"a="<<a<<"\tb="<<b<<endl; 
 swap(&a, &b); 
cout<<"After swapping\n"; 
cout<<"a="<<a<<"\tb="<<b<<endl; 
getch(); 

---------------×------------------××--------------------×---------------

48. Write a ‘C++’ program to find factorial of given number using loop.
Ans.
#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(); 

---------------×------------------××--------------------×---------------

49. Describe use of protected access specifier used in the class.
Ans. 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.

---------------×------------------××--------------------×---------------

50. Write any two characteristics of destructor.
Ans. 
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 never takes any argument. 
5. It does not return any value. 
6. It is invoked implicitly by the compiler upon exit from the program (or block or function) i.e when scope of object is over. 

---------------×------------------××--------------------×---------------

51. Write a C++ program to print fibonacci series  
Ans.
#include<iostream.h>
#include<conio.h>
void main()
{
    int a,b,c,n,i;
    clrscr();
    cout<<"\nEnter value = ";
    cin>>n;
    a=0;
    b=1;
    for(i=0;i<n;i++)
    {
        cout<<a<<" ";
        c=a+b;
        a=b;
        b=c;
    }
    getch();
}

---------------×------------------××--------------------×---------------
---------------×------------------××--------------------×---------------

C++ Assignments

---------------×------------------××--------------------×---------------

Assignment No. 1













---------------×------------------××--------------------×---------------

Assignment No. 2





---------------×------------------××--------------------×---------------

Assignment No. 3









---------------×------------------××--------------------×---------------

Assignment No. 4







---------------×------------------××--------------------×---------------

Practical No. 5





---------------×------------------××--------------------×---------------

OOP (313304) Manual 

OOP MANUAL (313304)⬇️


Embedded Webpage


---------------×------------------××--------------------×---------------

Practical No. 11











---------------×------------------××--------------------×---------------



---------------×------------------××--------------------×---------------



---------------×------------------××--------------------×---------------

Telegram Link 



---------------×------------------××--------------------×---------------
---------------×------------------××--------------------×---------------

Post a Comment

0 Comments