Professional Documents
Culture Documents
C++&CG Assignment 2
C++&CG Assignment 2
Assignment
1. Explain the features of CPP with suitable example.
ANSWER:
The following are the features of CPP object oriented programming language.
a) Objects:
b) Classes:
c) Data abstraction:
d) Data encapsulation:
e) Inheritance:
f) Polymorphism:
g) Overloading.
h) Reusability.
a) Objects: An entity that can store data and send and receive messages. An instance of
a class. Objects are basic building blocks for designing programs.
Ex. Program:
#include<iostream>
using namespace std;
class person
{
char name[20];
int id;
public:
void getdetails(){}
};
int main()
{
person p1; // p1 is a object
}
b) Classes: The entire set of data and code of an object can be made a user- defined data
type with the help of a class.
Object are variable of class.
Class is a collection of objects of similar type.
e.g. fruit mango
Objects are basic run time entities in object oriented system. They may represent a
person, account, place etc.
C++ &CG ASSIGNMENT: DATED: 26/10/2020
NAME:PANDURANG BHAGWAN PAWAR ROLL NO:A09 REG NO:2018BTT002
Ex. Program:
#include<iostream>
Class employee// class declaration
{
Private:
Char empname[50];
Int empno;
Public:
Void getvalue()
{
cout<<“INPUT Employee name:”;
cin>>empname;
cout<<“INPUT Employee number:”;
cin>>empno;
}
Void display value()
{
C
cout<<“Employee name:”<< empname<<endl;
cout<<“Employee number :”<< empno<<endl;
}
};
Main{}
{
Employee e1// Creation of Object
E1.getvalue();
E1.displayvalue();
}
c) Data abstraction: The insulation of data from class, function provides interface between
data and program.
Ex. Program:
#include <iostream>
using namespace std;
class implementAbstraction
{
private:
int a, b;
public:
// method to set values of
C++ &CG ASSIGNMENT: DATED: 26/10/2020
NAME:PANDURANG BHAGWAN PAWAR ROLL NO:A09 REG NO:2018BTT002
Op: a=10
b=20
d) Data encapsulation: The wrapping up of data and function into a single class is known
as encapsulation. The data is not accessible out of the class with help of encapsulation it
possible to access object out of the class.
The attributes are called data members.
Ex. program:
#include <iostream>
using namespace std;
class Employee {
private:
// Private attribute
int salary;
public:
// Setter
void setSalary(int s)
C++ &CG ASSIGNMENT: DATED: 26/10/2020
NAME:PANDURANG BHAGWAN PAWAR ROLL NO:A09 REG NO:2018BTT002
{
salary = s;
}
// Getter
int getSalary() {
return salary;
}
};
e) Inheritance: :- it is the process by which objects of one class acquired the properties of
objects of another classes.
Ex. Program:
// Base class
class Vehicle {
public:
string brand = "Ford";
void honk() {
cout << "Tuut, tuut! \n" ;
}
};
// Derived class
class Car: public Vehicle {
public:
string model = "Mustang";
};
int main() {
Car myCar;
myCar.honk();
cout << myCar.brand + " " + myCar.model;
return 0;
}
f) Polymorphism: it is a Greek term means the ability to take more than one form.
The process of making an operator to exhibit different behaviors in the different
instances is known as operator overloading.
Polymorphism plays an important role in allowing objects having different internal
structures to share the same external interface.
Ex. Program:
// C++ program for function overloading
C++ &CG ASSIGNMENT: DATED: 26/10/2020
NAME:PANDURANG BHAGWAN PAWAR ROLL NO:A09 REG NO:2018BTT002
#include <iostream>
using namespace std;
class myclass
{
public:
// function with 1 int parameter
void func(int x)
{
cout << "value of x is " << x << endl;
}
// function with same name but 1 double parameter
void func(double x)
{
cout << "value of x is " << x << endl;
}
// function with same name and 2 int parameters
void func(int x, int y)
{
cout << "value of x and y is " << x << ", " << y << endl;
}
};
int main() {
myclass obj1;
// Which function is called will depend on the parameters passed
// The first 'func' is called
obj1.func(7);
// The second 'func' is called
obj1.func(9.132);
// The third 'func' is called
obj1.func(85,64);
return 0;
}
g) overloading:
C++ &CG ASSIGNMENT: DATED: 26/10/2020
NAME:PANDURANG BHAGWAN PAWAR ROLL NO:A09 REG NO:2018BTT002
Ex. Program:
#include<iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i =0) {real = r; imag = i;}
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2; // An example call to "operator+"
c3.print();
}
h) reusability:
/***reusability**/
#include <cmath>
#include <stdio.h>
#include<iostream>
using namespace std;
float magnitude(int x, int y)
{
return sqrt(x * x + y * y);
}
int main(int argc, const char *argv[])
{
cout << magnitude(1, 2) << "\n";
cout << magnitude(2, 3) << "\n"; //reusability of function magnitude
}
OUTPUT: 2.23607
3.60555
2. Write a program swapping of values with the help of third variable.
C++ &CG ASSIGNMENT: DATED: 26/10/2020
NAME:PANDURANG BHAGWAN PAWAR ROLL NO:A09 REG NO:2018BTT002
ANSWER:
/*******Swap two values
using third variable******/
#include<iostream>
using namespace std;
int main()
{
int a,b,temp;
cout<<"Enter the value of a and b:"<<endl;
cin>>a>>b;
cout<<"Before Swap:\n"<<" A:"<<a<<"\n B:"<<b<<endl;
temp=a;
a=b;
b=temp;
cout<<"After Swap:\n"<<" A:"<<a<<"\n B:"<<b<<endl;
return 0;
}
int main()
{
int i, n;
float arr[100];
return 0;
}
if (n < 10)
if (n > 0)
cout << "The number is positive." << endl;
else cout << "The number is ______________." << endl;
A. What is the output of the statement if the variable n has the value 7 ?
If n has the value 15 ? If n has the value -3 ?
ANSWER:
If the variable n has the value 7 ,
the output will be “The number is positive.”
If n has the value 15 ,
The output will not display .
If n has the value -3 ,
The output will be “The number is ______________.”
B. Correct the syntax of the statement so that the logic of the corrected statement corresponds to
the formatting of the original statement. Also, replace the blank with an appropriate word or
phrase.
ANSWER:
#include<iostream>
int main()
{
int n;
cout<<"enter a number:"<<n;
cin>>n;
{
C++ &CG ASSIGNMENT: DATED: 26/10/2020
NAME:PANDURANG BHAGWAN PAWAR ROLL NO:A09 REG NO:2018BTT002
if (n<10)
{
if (n>10)
cout << "The number is positive." << endl;
if (n>0)
return 0;
}
}
C. Correct the formatting of the (original) statement so that the new format reflects the logical
behavior of the original statement. Also, replace the blank with an appropriate word or phrase.
ANSWER:
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"enter a number:"<<n;
cin>>n;
if (n < 10)
if (n > 0)
cout << "The number is positive." << endl;
else
cout << "The number is negative." << endl;
return 0;
}
n /= 2;
cout << n * n << endl;
ANSWER:
A. Output: 0
There are no curly brackets for the last two lines . The statement "n /= 2;"
of the loop should display the values 5, 2, 1, and 0 .
at but the "cout" statement will display 0 . The values 5, 2, and 1 will not be displayed.
taking curly brackets for the last two lines will display desired output.
B.
#include<iostream>
using namespace std;
main()
{
int n = 10;
while (n > 0)
{
n /= 2;
cout << n * n << endl;
}
return 0;
C.
#include<iostream>
using namespace std;
main()
{
int n = 10;
while (n > 0)
C++ &CG ASSIGNMENT: DATED: 26/10/2020
NAME:PANDURANG BHAGWAN PAWAR ROLL NO:A09 REG NO:2018BTT002
n /= 2;
cout << n * n << endl;
return 0;
A. What will be the output of the fragment above if the interactive user enters the
integer value 0 ?
B. What will be the output of the fragment above if the interactive user enters the
integer value 15 ?
C. What will be the output of the fragment above if the interactive user enters the
integer value 7 ?
D. What values for n will cause the output of the fragment above
to be "not interesting"?
ANSWER:
D. the output “not interesting.” Will not displayed for any value . That part of the program will
not be executed.
C++ &CG ASSIGNMENT: DATED: 26/10/2020
NAME:PANDURANG BHAGWAN PAWAR ROLL NO:A09 REG NO:2018BTT002
ANSWER:
The output of the code fragment is as follows:
n = 3
The fractional part of the floating point value is discarded when the value is cast to integer type.
#include <iostream>
using namespace std;
int main()
{
int n1, n2, GCD;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
9.
What is the output when the following code fragment is executed?
Rewrite the fragment to obtain an equivalent code fragment in which the body of
the loop is a simple statement instead of a compound statement.
int i = 5;
while (i > 0)
{
--i;
cout << i << endl;
}
ANSWER:
Output:
4
3
2
1
0
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int i;
for ( i=4;i >=0;i--)
cout << i << endl;
return 0;
}
Output:
4
3
2
1
0
C++ &CG ASSIGNMENT: DATED: 26/10/2020
NAME:PANDURANG BHAGWAN PAWAR ROLL NO:A09 REG NO:2018BTT002
Int n=1,sum=1;
While(sum<goal
Sum+=++n;
Return n;
ANSWER:
#include <iostream>
int n = 1, sum = 1 ;
sum += ++n;
return n;
}
C++ &CG ASSIGNMENT: DATED: 26/10/2020
NAME:PANDURANG BHAGWAN PAWAR ROLL NO:A09 REG NO:2018BTT002
int main(){
int g;
cin>>g;
return 0;
Output:
10