402 CPP
402 CPP
402 CPP
is called the
extraction operator. It gets the value from the stream object cin. insertion- operator << (or the left shift
operator in c) is called insertion operator. #it directs the from stream object count.
#Explain any two manipulators. ===1) setting of field width = setw() is used to define the width of field necessary for
the output for a variable. Eg. setw (5); Cout << 123; ii) Setting precision for flout nos:
=we Can Control number of digits to be displayed. after decimal point for float numbers by using set precision()
function. eg: set precision (2),
#detine Constructor ===1) A constructor is a special a member function which is used to allocate memory space and
values to the data member of the object. ii) A Constructor makes the object functional by converting an object with
the unused memory into a usable object.
#What is default argument. ===CH allows to define an argument whose valve will be automatically used by the
compiles, if it is not provided during the function call, this argument is called as default arguments.
#What is the use of scope resolution operator. ===1) The Scope resolution operator is denoted by a pair of Colons (J.)
2) When the same function name is used for many classes, then this Complexity is solved by using scope resolution.
operator to indicate the class with which the function is associated.
#What are the access specifiers used in C++. ===1) Access specifiers. are used to restrict the accessibility of class
members. Access specifiers define how the class be accessed. ii) In the class declaration syntax, the key words
private public and protected are known as access specifiers.
#Exception Handling. ===1) C++ exception handling mechanism uses there keywords: toy catch and throw.
2)toy block contains program statements that we want tomonitor for exceptions. 3) when an exception is detected it
is thrown using it how statement. in the try block 4) The exception is fought using catch. typically, a catch (.) black
is used to loy errors & perform special clean up before program execution is stopped.
#How a function work? ===1)A C++ program does not execute the statement in a function until the function is called
or invoked. 2)When the C++ program's function is called or invoked, the control passes to the function and
returns back to the calling part after the execution of function is over or exit. 3)The calling program can send
information to the functions in the form of argument. An argument of function stores data needed by the function to
perform its task.
#Explain memory management operators with the help of suitable example. ===1) for dynamic memory allocation,
C++ provides new operator. in we have malloc() functions for dynamic allocation. ii) for dynamic allocation, C++
provides delete () operators, which Same as tree () faction in C. 1) me new operator = This operator is used
for allocution of memory Syntax- ptr-var = new datatype; 2) Delete operator :- The delete operator
deallocates or tree the memory allocated by new. Example = #include <iostream>
#include <strong>
int main() {
char *x;
x= new chor [10]
strcpy (*, "Computer ");
cout<<"\n the string is:"<<x<< end/;
delete xi
return 0; 3
out put-The string is Computer.
#Explain memory allocation for objects with non-static data member and static data member. ===1) static data
members = in C++, a data member of a class con be qualified as static. variables are normally used to maintain values
Common to the entire Class. ii) non-static data members = when writing a class that has multiple constructor,
having to specify default values for all members in each constructor results in redundant Code.
#What is meant by oop? ===Object Oriented Programming (OOP) is playing an increasingly significant role in the
analysis, design and implementation of software systems. OOP languages provide the programmer the ability to
create class hierarchies, instantiate objects and send messages between objects to process themselves.
#when do we make with suitable example. a class virtual base class? ===1) Con Consider a situation where multilevel,
multiple and hierachicol all the tree kinds of inheritance are involved. ii) To resolve this ambiguity, C++ includes a
mechanism by which only one Copy of Base will be included in Derived 3. This feature is called a virtual base class..
example -#include <iostream>
class buse {
Public:int x; };
class derived 1 virtual by public buse {
Public:int z; }; class derived 2: virtual public buse {
Public: class derived 3: public derived, public derived 2 {
Public: int mult (
{ return x *y #z; } };
void main () {
derived d
d. x = lo
d.y = 20; d. 2 = 30)h
cout << "product is "<<d- mult (); }
#Explain array of object in C++ with example. ===1) The array at variable of class data type is called at array objects.
2)An array of objects is also known as an any is an array of a class type of objects. An array of objects is declared in
the some way as an array of any built-in data type. iii) The syntax for declaring an away of objects is as
follows: class-name array-name [size]; (iv) Example: class student
{
int roll;
char name [20];
public:
void get data ();
void put data();
#write a C++ program to create a class which contains two data members, write member function to accept display &
Swap two entered numbers using call by reference.===
# include <iostream> int temp = nam1;
using namespace std; num1= num2;
class number { hum2= temp;
Private: cout << "numbers swapped! " << num1) << " & " <<
int num1, num2; num2 << end/; }
Public: };
void accept numbers() { int main(){
Cout<<"Enter two numbers."; number n;
cin >> num1 >> num2;} h. accept numbers ();
void & display numbers () { h. Swap numbers ();
Cout << Numbers entered!" << num1 << " & "<< return 0; }
num2<<end/;}
Void swop numbers () {
#Operator overloading. ===1) operator oven loading is closely related to function overloading. ii) in c++ you con
overload most operators so that they perform. special operations relative to classes that you create. iii) Actually
c++ tries. to make use. defined data types behave in much that some way as built-in types.. iv) This means that C+
+ bus the ability to provide the operators with a special meaning for data type. The mechanism op of giving such
special meanings to an operator is known Las operator overloading!
#What is token? ===1)The smallest individual units in a program are known as Tokens. 2)A token is a group of
characters that logically belong together. 3)Token is a sequence of characters from the character set of C++,
which is identified by the compiler.
# write a C++ Program to read Contents of a text file and Count number of characters words & line in a file. ===
#include < iostream> if (c == " || c == '\n') {
#include <f stream> word Count++; }
#include<string> it (c== '\n'){
using name space std; line count++; }
int main () { }
ifstream infile: it (C1 = " && c != '\n') {
int char count=0, word count = 0, line Count = 0; word Count ++; }
char C; Cout<<" Number of characters." << char cunt end/;
in File. Open (file, txt"); Cut <<" Number of words: " << word Count << end/;
if (linFile ){ cout<2" number of lines." << line Count << end/;
Cerr LL" Error: File not found \n"; if file infile. case();
return 1; } return 0;}
while while(inFile. get (c)) {
Chur Count++;
#What is c++. ===1)C++ is a hybrid language, in which some entities are objects and some are not.
2)C++ is an extension of C language, implemented not only to add object-oriented capabilities but also to readdress
some of the weaknesses of C language.
#what is class template? Explain syntax of class template. with suitable example? ===1) template are a feature of the
C++ programing language that. allow functions & classes to operate with generic types. This. allows a function or
class to work or many different date types without being rewritten for each one. 2) Templates can be used in
Conjunction with abstract datatype
in order to allow them to handle & any type of data
iii) The basic syntax. Template <class a-types> class a-class {……};
iv) example= template <class T>
class arr
{T a[10] ;
int I;
public;
void add (const T & ele);
t remove (void);
Void shov(); };
#identifier ===1)An Identifier is any name of variables, functions, classes etc. given by the programmers.
2)Identifiers are the fundamental requirement of any language. 3)The identifier is a sequence of characters taken
from C++ character set.
#Variable ===1)A variable is a named location in memory that is used to hold a value that may be modified by the
program. 2)Variable is the data name that refers to the stored value. 3)A variable name is an identifier
or a symbolic name assigned to memory location where data is stored. 4)A variable provides us with named
storage that our programs can manipulate.
#Function ===1)Functions are the basic building blocks of C++ and the place where all program activity occurs. A
function is a group of statements that together perform a task. 2)A function is a subprogram that acts on the
program data and often returns values. 3)A program written with numerous functions is easier to maintain,
update and debug than one very long or large program.
#What is setf() function? === The function setf () sets the io stream format flag s of the current stream to flags. The
optional needed argument specifies that only the flags that are in both flags and needed should be set. The return
value is the previous configuration of io stream format flag s.
#What is mean by copy constructor? == 1)A copy constructor takes a reference to an object of the same class as itself
as an argument. 2)A constructor that initializes an object using values of another object passed to it as
parameter, is called copy constructor.
#What is static data member? === Static data members are class members that are declared using static keywords. A
static member has certain special characteristics which are as follows: Only one copy of that member is created for
the entire class and is shared by all the objects of that class, no matter how many objects are created.
#constructor ===1) A constructor is a special member function which is used to allocate memory space and values to
the data members of that object. 2) A constructor makes the object functional by converting an object with the
unused (uninitialized) memory into a usable (initialized) object. 3)Whenever, an object is created, the constructor
will be executed automatically and initialization of data member takes place.
#What is virtual base class? === the base class is inherited by both Derived1 and Derived2. Derived3 directly inherits
both Derived1 and Derived2. All the public and protected members of Base are inherited into Derived3 twice through
both Derived1 and Derived2. Therefore, Derived3 would have duplicate sets of members inherited. This causes
ambiguity when a member of Base is used by Derived3.
#What is meant by ‘this’ pointer? === 1)Every object in C++ has access to its own address through an important
pointer called 'this' pointer. 2)In other words, "The member functions of each and every object have access to a
pointer named 'this', which points to the object itself. 3) The 'this' pointer is an implicit parameter to all
member functions. Therefore, inside a member function, this may be used to refer to the invoking object.
#What is function overloading? ===1) C++ provides the facility of function overloading in which we can define
multiple functions with the same name but the types of argument are different. For example, we can have a
function add with two floating point numbers as argument, another function add with 3 integers, one add function
with 4 doubles and so on. But function name is similar. This is called as Function Overloading.
#What is stream? === It is a derived class of ios and hence, inherits the properties of ios. It defines input functions
such as get(), getline() and read( ). In addition, it has an overloaded member function, stream extraction operator >>,
to read data from a standard input device to the memory items.
#What is meant by manipulators? === 1)Manipulators are special functions that can be included in the I/O
statements to alter the format parameters of a stream. 2)To access these manipulators, the file iomanip.h has to
be included in the program. Manipulators perform formatting tasks. They provide the same features as IOS 6
3)member functions and flags. But some manipulators are more convenient to use. We can concatenate several
manipulators together.
#file===1)The file handling techniques of C++ support file manipulation in the form of stream objects. The stream
object cin and cout are used extensively to deal with the standard input and output devices. 2)The C++
input/output system support a hierarchy of classes that are used to manipulate both the console and disk files, called
stream classes.
#Input stream === Input stream is declared as a class ifstream. It is derived from istream. Output stream is declared
as a class ofstream. It is derived from ostream. Input/Output stream should belong to fstream class. It is derived from
iostream.
#What is a function template? === 1)C++ provides a facility for function templates that allow you to write one
function which is act as template for a number of functions which are performing similar tasks. 2)A function
template will not use actual type of arguments but it uses generic type as a placeholder implicit when we make a call
to a function. For each version of data type, the compiler generates a copy of that function. For example: when
integer version is called, the compiler generates "add" function with integer data type and so on.
#Explain exception handling. === When we work with program or compile the program, we normally get logic errors
or syntactic errors. We can detect these errors by debugging. We often come across the errors other than logic and
syntactic errors. They are known as exceptions. Exceptions are run-time errors that a program encounters while
executing. For example, Array out of bounds, divide by zero. C++ provides a built-in error handling mechanism that
is called Exception Handling. Using exception handling you can more easily manage and respond to run-time errors.
#Write short note on: template. === A template is a simple yet very powerful tool in C++. The simple idea is to pass
the data type as a parameter so that we don’t need to write the same code for different data types. For example, a
software company may need to sort () for different data types.
#Explain various stream class used to perform Console i/o operator. ===
i) The i/o system in c++ is design to work with a wide variety of devices including terminals, disk & tope drivees.
ii) The source stream, which provides data to program, is called input stream.
iii) the destination stream that receives output from the program is called as output stream.
#Write short note on: Overloading a function template. === We can use overloaded function template, in the
situation where all possible data types cannot be handled using function templates. A template function may be
overloaded either by template functions or ordinary functions of its name. Overloading in such a case is as follows:
1. Call an ordinary function with same name. 2. Call a template function with the same name.
Constructor Destructor
Constructor helps to initialize the object of a class. Whereas destructor is used to destroy the instances.
Constructor can either accept arguments or not. While it can’t have any arguments.
#What is destructor? === A destructor is a special member function in C++ that is called automatically when an
object is destroyed. It is the last function that is called before an object is removed from memory. The purpose of a
destructor is to release any resources that were allocated by the object during its lifetime.
#Explain object as function argument? Explain with help of an example program. ===1) In C++, objects can be passed
as function arguments. This is useful when we want to initialize all data members of an object with another object,
or when we want to modify the state of an object within a function. 2)When an object is passed as an
argument to a function, a copy of the object is created and passed to the function. This copy is destroyed when the
function returns. If the object is large, this can be inefficient in terms of memory usage and performance.
####Write a c++ program to calculate factorial of integer number by using inline function. ===
#include <iostream>
using namespace std;
inline int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
int main() {
int num;
cout << "Enter a positive integer: ";
cin >> num;
cout << "Factorial of " << num << " is " << factorial(num) << endl;
return 0; }
#Design C++ class which contains function count (). write a Program to count number of time count () is called. (use
static data member). ===
#include <iostream> };
using namespace std; int Counter::count = 0;
class Counter { int main() {
static int count; Counter obj;
public: obj.count();
void count() { obj.count();
count++; obj.count();
} cout << "Count: " << Counter::getCount() << endl;
static int getCount() { return 0;
return count; }
}
#State advantages and disadvantages of c++. === Advantages:1)C++ is a powerful language that offers low-level
memory manipulation and high-level abstractions. 2)C++ is a compiled language, which means that it can be
highly optimized for performance. 3)C++ supports object-oriented programming, which allows for code reuse
and modular design. 4)C++ is a portable language, which means that it can be compiled and run on a wide
variety of platforms. Disadvantages: 1)C++ can be difficult to learn and use, especially for beginners. 2)C+
+ can be error-prone due to its low-level memory manipulation features. 3)C++ can be verbose and require
more code to accomplish simple tasks compared to other languages. 4)C++ can be less secure than other
languages due to its low-level memory manipulation features.
#Explain tellg()and tellp() with syntax. === tellg() and tellp() are member functions of the istream and ostream
classes respectively in C++. They are used to get the current position of the get and put pointers in the stream.
The syntax for tellg(): pos_type tellg(); Here, pos_type is a member type of the istream class that
represents the current position of the get pointer. If the pointer points to a valid position, then this function returns
the current position of the get pointer. Otherwise, it returns -1. The syntax for tellp(): pos_type tellp();
#What is static polymorphism. === Static polymorphism is achieved through the useof templates and function
overloading. In both cases, the compiler generates code for each specific type or set of arguments used in the
program, resulting in faster execution times compared to dynamic polymorphism
#What are the visibility lables used in C++. === In C++, visibility labels are used to control the accessibility of class
members. There are three visibility labels in C++: public, protected, and private. 1)public: Members declared as
public are accessible from anywhere in the program. 2)protected: Members declared as protected are
accessible from within the class and its derived classes. 3)private: Members declared as private are only
accessible from within the class.
###Write a C++ program to create a class Which contains two data members. Write member function to accept ,
display and swap two entered numbers using call by reference. ===
#include <iostream> num1 = num2;
using namespace std; num2 = temp;
class MyClass { }
int num1, num2; }
public: int main() {
void accept() { MyClass obj;
cout << "Enter two numbers: "; obj.accept();
cin >> num1 >> num2; cout << "Before swapping:" << endl;
} obj.display();
void display() { obj.swap();
cout << "Number 1: " << num1 << endl; cout << "After swapping:" << endl;
cout << "Number 2: " << num2 << endl; obj.display();
} return 0;
void swap() { }
int temp = num1;
#What is abstraction and Encapsulation? === Abstraction =In object-oriented programming, abstraction is
the process of hiding the implementation details of an object and only showing its essential features to the
user. It is a way of reducing complexity by breaking down a system into smaller, more manageable parts.
Encapsulation =On the other hand, encapsulation is the process of bundling data and functions that
operate on that data into a single unit, such as a class in C++. Encapsulation provides a way to protect data
from outside interference and misuse by making it accessible only through the methods defined in the
class.
#What is pure virtual function and explain with the help of example program. === A pure virtual function is
a virtual function that has no implementation in the base class and is intended to be overridden by derived
classes. A pure virtual function is declared by assigning the value 0 to it. A class containing at least one pure
virtual function is called an abstract class. An abstract class cannot be instantiated and must be subclassed.
#Write a program to overload binary + operator to add two strings. ===
#include <iostream> String operator+(const String& s) const {
#include <cstring> String temp;
using namespace std;[9 delete[] temp.str;
class String { temp.str = new char[strlen(str) + strlen(s.str) +
char* str; 1];
public: strcpy(temp.str, str);
String() { strcat(temp.str, s.str);
str = new char[1]; return temp;
str[0] = '\0'; }
} void display() const {
String(const char* s) { cout << str << endl;
str = new char[strlen(s) + 1]; }
strcpy(str, s); };
}
String(const String& s) { int main() {
str = new char[strlen(s.str) + 1]; String s1("Hello"), s2("World"), s3;
strcpy(str, s.str); s3 = s1 + s2;
} s3.display();
~String() { return 0;
delete[] str; }
}
######Write C++ program to copy the contains of a text file into another text file. ===
#include <iostream> }
#include <fstream> if (!fout) {
using namespace std; cout << "Error opening output file." << endl;
int main() { return 1;
string input_file, output_file; }
cout << "Enter the name of the input file: "; char ch;
cin >> input_file; while (fin.get(ch)) {
cout << "Enter the name of the output file: "; fout.put(ch);
cin >> output_file; }
ifstream fin(input_file); cout << "Contents copied successfully." << endl;
ofstream fout(output_file); fin.close();
if (!fin) { fout.close();
cout << "Error opening input file." << endl; return 0;
return 1; }
#Write a C++ program to create a class customer which contains data members as C_id, C_Salary. Write
member functions to accept and display customer information , also display information of customer
having maximum salary. ===
#include <iostream> }
using namespace std; };
class customer { int customer::max_id = 0;
int C_id; float customer::max_salary = 0;
float C_Salary; int main() {
static int max_id; int n;
static float max_salary; cout << "Enter number of customers: ";
public: cin >> n;
void accept() { customer c[n];
cout << "Enter customer ID: "; for (int i = 0; i < n; i++) {
cin >> C_id; cout << "Enter details of customer " << i + 1 <<
cout << "Enter customer salary: "; ":" << endl;
cin >> C_Salary; c[i].accept();
if (C_Salary > max_salary) { }
max_salary = C_Salary; cout << endl;
max_id = C_id; for (int i = 0; i < n; i++) {
} cout << "Details of customer " << i + 1 << ":" <<
} endl;
void display() { c[i].display();
cout << "Customer ID: " << C_id << endl; cout << endl;
cout << "Customer salary: " << C_Salary << }
endl; customer::display_max();
} return 0;
static void display_max() { }
cout << "Customer with maximum salary: " <<
endl;
cout << "Customer ID: " << max_id << endl;
cout << "Customer salary: " << max_salary <<
endl;
#What is inheritance and explain the hierarchical inheritance. === Inheritance is a feature of object-
oriented programming that allows a new class to be based on an existing class. The new class inherits the
properties and methods of the existing class, which can then be modified or extended as needed.
Inheritance is used to create a hierarchy of classes that share common attributes and behaviors.
Hierarchical inheritance is a type of inheritance in which more than one subclass inherits the properties of
a single base class. In other words, there is one base class and multiple derived classes. Several other
classes can inherit the derived classes as well. Hierarchical structures thus form a tree-like structure.
#Explain structure of C++ program. ===The structure of a C++ program is made up of multiple source code
files that cater to different components such as main function, member functions, class definition, headers/
standard headers, comments, variables, data types, namespaces, input/ output statements, etc