C++ Internal
C++ Internal
Ans 1:-
#include<iostream>
using namespace std;
class Demo{
private :
int a;
public :
int b;
public:
void prot(float e1)
{
e=e1;
}
};
class Stud : public Demo{
public:
void details()
{
cout<<"Protected variable = "<<e<<endl;
}
};
int main()
{
Demo d;
d.setdata(21);
d.b =9;
d.getdata();
Stud s;
s.prot(4.1f);
s.details();
return 0; }
Output
Ans 3:-
A destructor is a special member function that is called automatically when an
object goes out of scope or when we delete the object with the delete
expression.
In C++, a destructor has the same name as that of the class, and it does not
have a return type. ~ precedes the identifier to indicate destructor.
A destructor function is called automatically when the object goes out of scope or
is deleted. Following are the cases where destructor is called:
-> Destructor is called when the function ends.
-> Destructor is called when the program ends.
-> Destructor is called when a block containing local variables ends.
-> Destructor is called when a delete operator is called.
Example of Destructor :-
#include<iostream>
using namespace std;
class demo
{
public:
demo()
{
cout<<"I am constructor"<<endl;
}
~demo()
{
cout<<"I am destructor";
}
};
int main()
{
demo d;}
Output ->
Ans 4->
int main() {
cout << sum(10, 15) << endl; // x = 10, y = 15, z = 0, w = 0
cout << sum(10, 15, 25) << endl; // x = 10, y = 15, z = 25, w = 0
cout << sum(10, 15, 25, 30) << endl; // x = 10, y = 15, z = 25, w = 30
return 0;
}
Output
25
50
80
Ans 6 :-
#include <iostream>
using namespace std;
class Factorial {
int number;
long long fact;
public:
// Constructor that calculates factorial
Factorial(int n) {
number = n;
fact = 1;
for (int i = 1; i <= number; i++) {
fact=fact*i;
}
}
// Method to display the factorial result
void display(){
cout << "Factorial of " << number << " is: " << fact <<
endl;
}
};
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
// Create an object of Factorial, which automatically
computes the factorial
Factorial f(num);
f.display();
return 0;
}
Output
Output
Program
Ans 2:- In C++, file operations are done using file stream
classes such as ifstream, ofstream, and fstream, which handle
the reading and writing of files. Let's dive into the explanation
of the specific file operations you've mentioned:
(a) open()
The open() function is used to open a file for reading, writing,
or both. When a file is opened successfully, it is associated with
a file stream object, and operations like reading or writing can
be performed on it.
Syntax:
ifstream infile; // Input file stream
ofstream outfile; // Output file stream
-> Open() takes the file name as a parameter and an optional mode flag
(ios::in, ios::out, ios::app, etc.).
-> If the file cannot be opened (e.g., it doesn't exist for reading or there
are permission issues), the open() method sets the file stream object to a
"failed" state.
(b) read()
The read() function is used to read data from a file in binary mode. It is
often used when reading raw data (e.g., integers, structures, or arrays)
from a file.
Syntax:
ifstream infile;
infile.read(reinterpret_cast<char*>(&buffer), sizeof(buffer));
The close() function is used to close a file once file operations are
completed (whether reading or writing). Closing a file is essential
because it ensures that all resources (such as file handles) are released
properly and that any data is written to disk.
Syntax:
infile.close(); // Close the input file
outfile.close(); // Close the output file
(d) EOF
Syntax:
ifstream infile;
while (!infile.eof()) {
// Read data}
Ans 3:- #include<iostream>
using namespace std;
class complex{
int x,y;
public:
complex(int real,int img)
{ x=real;
y=img; }
void output()
{ cout<<"X="<<x;
cout<<"Y="<<y<<endl; }
int main(){
complex ob1(10,20);
complex ob2(30,40);
complex ob3(10,30);
ob3=ob2+ob1; //valid method
ob1.output();
ob2.output();
ob3.output();
return(0);
}
Output ->>
Ans 4 :-
Example of constructor :-
Types of constructor :-
1.Default Constructor
A default constructor is a constructor that takes no parameters.
It is called automatically when an object is created without any
arguments.
If you don't define any constructor in a class, the compiler
provides a default constructor that initializes members to
default values.
If you define a constructor with parameters, and do not
define a default constructor, the compiler won't provide
one.
2. Parameterized Constructor
A parameterized constructor is a constructor that accepts one
or more arguments. It allows the user to initialize the object with
custom values at the time of object creation.
3. Copy Constructor
A copy constructor is used to create a new object as a copy of
an existing object. It is called when:
A new object is created from an existing object of the same
class (e.g., passing an object by value, returning an object
by value from a function).
The default copy constructor performs a shallow copy, but
you can define a custom copy constructor for a deep copy
(when an object contains pointers).
Ans 5 :- (A) Polymorphism
(B) Abstract Class
By definition, a C++ abstract class must include at least one pure
virtual function. Alternatively, put a function without a definition.
Because the subclass would otherwise turn into an abstract class
in and of itself, the abstract class's descendants must specify the
pure virtual function.
Broad notions are expressed using abstract classes, which can
then be utilized to construct more specific classes. You cannot
make an object of the abstract class type. However, pointers and
references can be used to abstract class types. When
developing an abstract class, define at least one pure virtual
feature. A virtual function is declared using the pure specifier (= 0)
syntax.
Consider the example of the virtual function. Although the class's
objective is to provide basic functionality for shapes, elements of
type shapes are far too general to be of much value. Because of
this, the shape is a good candidate for an abstract class:
(C)Nested Class
A nested class in C++ is a class defined within another class. The nested
class has the scope of the outer class, and it can access the members
(both private and public) of the outer class, if explicitly allowed. A
nested class is often used when the inner class is closely related to the
outer class and isn't meant to be used outside of it.
1. Static Nested Class: The inner class does not have access to the
non-static members of the outer class.
2. Non-static Nested Class: The inner class has access to the non-
static members of the outer class (i.e., it behaves like a regular
member function).
Syntax:
class OuterClass {
class NestedClass {
// Nested class definition
};
};
(D) Namespace
A namespace is designed to overcome this difficulty and is used
as additional information to differentiate similar functions,
classes, variables etc. with the same name available in different
libraries. Using namespace, you can define the context in which
names are defined. In essence, a namespace defines a scope.
(E) Templates
Ans 6 :-
#include <iostream>
#include <cmath>
using namespace std;
class AreaCalculator {
public:
int main()
{
AreaCalculator calculator;
double length = 5.0, width = 10.0;
double side1 = 3.0, side2 = 4.0, side3 = 5.0;
double radius = 7.0;
Output