Introduction To Python
Introduction To Python
Introduction To Python
Solution Set
• Real-time system
• Simulation and modeling
• Object-oriented data bases
• Hypertext, Hypermedia, and expertext
• AI and expert systems
• Neural networks and parallel programming
• Decision support and office automation systems
• CIM/CAM/CAD systems
b. Write a C++ program to create a class Bank with { acno, custname, bal} as its
attributes. And implement the methods withdraw() , deposit() and showBalance().
Class Bank
{
private: intaccno; char custname[40];double bal;
public:
void withdraw()
{ Define Method // }
void deposit()
{ Define Method // }
void showBalance()
{ Define Method // }
};
void main()
{ Bank obj;
obj.withdraw(); obj.deposit(); obj.showBalance();
}
c. Explain in brief the concept of friend function and class with suitable example.
lass Box {
double width;
public:
friend void printWidth( Box box );
void setWidth( double wid );
};
return 0;
}
d. What is constructor? State its characteristics.
A constructor (having the same name as that of the class) is a member function which
is automatically used to initialize the objects of the class type with legal initial values.
These have some special characteristics. These are given below:
(i) These are called automatically when the objects are created.
(ii) All objects of the class having a constructor are initialized before some use.
(iii) These should be declared in the public section for availability to all the functions.
(iv) Return type (not even void) cannot be specified for constructors.
(v) These cannot be inherited, but a derived class can call the base class constructor.
(vi) These cannot be static.
(vii) Default and copy constructors are generated by the compiler wherever required.
Generated constructors are public.
(viii) These can have default arguments as other C++ functions.
(ix) A constructor can call member functions of its class.
(x) An object of a class with a constructor cannot be used as a member of a union.
(xi) A constructor can call member functions of its class.
(xii) We can use a constructor to create new objects of its class type by using the
syntax
int main()
{
long a, b, x;
float c, d, y;
x = add(a, b);
y = add(c, d);
sum = x + y;
return sum;
}
sum = x + y;
return sum;
}
classBox{
double length;// Length of a box
double breadth;// Breadth of a box
double height;// Height of a box
public:
doublegetVolume(void){
return length * breadth * height;
}
voidsetLength(doublelen){
length =len;
}
voidsetBreadth(doublebre){
breadth =bre;
}
voidsetHeight(doublehei){
height =hei;
}
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume =Box1.getVolume();
cout<<"Volume of Box1 : "<< volume <<endl;
// volume of box 2
volume =Box2.getVolume();
cout<<"Volume of Box2 : "<< volume <<endl;
// volume of box 3
volume =Box3.getVolume();
cout<<"Volume of Box3 : "<< volume <<endl;
return0;
}
c. List the operators that cannot be overloaded. Explain the rules for overloading the
operators.
Ans:
Operators that cannot be overloaded::: , .*, . , ?:
Rules:
1. Only existing operators can be overloaded. New operators cannot be overloaded.
2. The overloaded operator must have at least one operand that is of user defined type.
4. Overloaded operators follow the syntax rules of the original operators. They cannot
be overridden.
5. There are some operators that cannot be overloaded like size of operator(sizeof),
membership operator(.), pointer to member operator(.*), scope resolution operator(::),
conditional operators(?:) etc
8. Binary operators overloaded through a member function take one explicit argument
and those which are overloaded through a friend function take two explicit arguments.
9. When using binary operators overloaded through a member function, the left hand
operand must be an object of the relevant class.
10. Binary arithmetic operators such as +,-,* and / must explicitly return a value. They
must not attempt to change their own arguments.
Ans: When we declare a member of a class as static it means no matter how many
objects of the class are created, there is only one copy of the static member.
A static member is shared by all objects of the class. All static data is initialized to zero
when the first object is created, if no other initialization is present. We can't put it in
the class definition but it can be initialized outside the class as done in the following
example by redeclaring the static variable, using the scope resolution operator :: to
identify which class it belongs to.
classBox{
public:
staticintobjectCount;
// Constructor definition
Box(double l =2.0,double b =2.0,double h =2.0){
cout<<"Constructor called."<<endl;
length = l;
breadth = b;
height = h;
private:
double length;// Length of a box
double breadth;// Breadth of a box
double height;// Height of a box
};
int main(void){
// Print total number of objects before creating object.
cout<<"Inital Stage Count: "<<Box::getCount()<<endl;
return0;
}
• Pure Virtual functions can be given a small definition in the Abstract class,
which you want all the derived classes to have. Still you cannot create object of
Abstract class.
• Also, the Pure Virtual function must be defined outside the class definition. If
you will define it inside the class definition, complier will give an error. Inline
pure virtual definition is Illegal.
In this type of inheritance the derived class inherits from a class, which in turn inherits
from some other class. The Super class for one, is sub class for the other.
b. Write a C++ program to implement the following hierarchy of inheritance.
Lion Tiger
Animal
Ans:
Class Lion
{
};
Class Tiger
{
};
Class Animal : public Lion , Tiger
{
};
int main () {
inti = 39;
int j = 20;
cout<< "Max(i, j): " << Max(i, j) <<endl;
double f1 = 13.5;
double f2 = 20.7;
cout<< "Max(f1, f2): " << Max(f1, f2) <<endl;
string s1 = "Hello";
string s2 = "World";
cout<< "Max(s1, s2): " << Max(s1, s2) <<endl;
return 0;
}
int main() {
try {
Stack<int>intStack; // stack of ints
Stack<string>stringStack; // stack of strings
The mode parameter specifies the mode in which the file has to be opened.
d. Write a C++ program to read the input from the user and write into the file. [Select a
suitable file mode]
#include <fstream>
#include <iostream>
#include <string>
usingnamespacestd;
int main()
{
string sentence;
string mysent;
e. Write a C++ program to display the contents from the file in a console mode. [Select a
suitable file mode]
#include <stdio.h>
#include <stdlib.h> // For exit()
intmain()
{
FILE*fptr;
charfilename[100], c;
// Open file
fptr = fopen(filename, "r");
if(fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
fclose(fptr);
return0;
}
f. Write a C++ program to copy the contents from one file to other file. [Select a suitable
file mode]
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
ofstreamfout;
ifstream fin;
inti=0;
int n;
char ch[20];
clrscr();
fout.open("a.txt");
cout<<"enter no. of lines";
cin>>n;
cout<<"\nenter contents to first file\n\n";
do
{
cin.getline(ch,20);
fout<<ch;
fout<<"\n";
i++;
}while(i<=n);
fout.close();
fin.open("a.txt");
fout.open("a.txt");
cout<<"\ncontents of second file\n";
while(fin.eof()==0)
{
fin.getline(ch,20);
fout<<ch;
fout<<"\n";
}
fout.close();
fin.close();
fin.open("a1.txt");
while(fin.eof()==0)
{
fin.getline(ch,20);
cout<<ch;
cout<<"\n";
}
fin.close();
getch();
}
_____________________________