Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

C++ &CG ASSIGNMENT: DATED: 26/10/2020

NAME:PANDURANG BHAGWAN PAWAR ROLL NO:A09 REG NO:2018BTT002

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

        // private members


        void set(int x, int y)
        {
            a = x;
            b = y;
        }
          
        void display()
        {
            cout<<"a = " <<a << endl;
            cout<<"b = " << b << endl;
        }
};
  
int main() 
{
    implementAbstraction obj;
    obj.set(10, 20);
    obj.display();
    return 0;
}

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

// This is automatically called when '+' is used with


// between two Complex objects
Complex operator + (Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};

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

3. Write a program to find largest number in a given array.


ANSWER:
//****to display largest number in a given array****/
#include <iostream>
using namespace std;

int main()
{
int i, n;
float arr[100];

cout << "Enter total number of elements(1 to 100): ";


cin >> n;
cout << endl;

for(i = 0; i < n; ++i)


{
cout << "Enter Number " << i + 1 << " : ";
cin >> arr[i];
}

for(i = 1;i < n; ++i)


{
C++ &CG ASSIGNMENT: DATED: 26/10/2020
NAME:PANDURANG BHAGWAN PAWAR ROLL NO:A09 REG NO:2018BTT002

if(arr[0] < arr[i])


arr[0] = arr[i];
}
cout << "Largest element = " << arr[0];

return 0;
}

4. What will be the output of given code?

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>

using namespace std;

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)

cout<<"The number is positive"<<endl;


}
else
cout<<"the number is greater than or equal to 10."<<endl;

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

5. Answer the following


The loop shown below has been written by an inexperienced C/C++ programmer. The behavior of the
loop is not correctly represented by the formatting.
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;

A. What is the output of the loop as it is written?


B. Correct the syntax of the loop so that the logic of the corrected statement corresponds to
the formatting of the original loop. what is the output of corrected loop?
C. Correct the formatting of the (original) loop so that the new format reflects the logical behavior
of the original loop.

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;

Output of corrected loop:


25
4
1
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;

6. Answer the following


int n;
cout << "Enter an integer: ";
cin >> n;
if (n < 10)
cout << "less than 10" << endl;
else if (n > 5)
cout << "greater than 5" << endl;
else cout << "not interesting" << endl;

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:

A. The output will be “less than 10.”


B. The output will be “greater than 15”.
C. The output will be “less than 10”.

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

7. What will be the output:


int n;
float x = 3.8;
n = int(x);
cout << "n = " << n << endl;

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.

8. Write a program to find the gcd number of a given number.


ANSWER:

#include <iostream>
using namespace std;

int main()
{
int n1, n2, GCD;
cout << "Enter two numbers: ";
cin >> n1 >> n2;

// Swapping variables n1 and n2 if n2 is greater than n1.


if ( n2 > n1)
{
int temp = n2;
n2 = n1;
n1 = temp;
}

for (int i = 1; i <= n2; ++i)


{
if (n1 % i == 0 && n2 % i ==0)
{
GCD = i;
}
}

cout << "GCD OR HCF = " <<GCD;


return 0;
C++ &CG ASSIGNMENT: DATED: 26/10/2020
NAME:PANDURANG BHAGWAN PAWAR ROLL NO:A09 REG NO:2018BTT002

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

10. What will be the output of following code.


Int enough(int goal)

Int n=1,sum=1;

While(sum<goal

Sum+=++n;

Return n;

ANSWER:

#include <iostream>

using namespace std;

int enough (int goal)

int n = 1, sum = 1 ;

while (sum < goal)

cout<<"sum before increment"<<sum<<endl;

sum += ++n;

cout<<"sum after increment"<<sum<<endl;

return n;

}
C++ &CG ASSIGNMENT: DATED: 26/10/2020
NAME:PANDURANG BHAGWAN PAWAR ROLL NO:A09 REG NO:2018BTT002

int main(){

int g;

cout<<"enter goal value"<<g<<endl;

cin>>g;

cout<<"output of enough function is:"<<enough(g)<<endl;

return 0;

Output:

Enter goal value0

10

sum before increment:1

sum after increment:3

sum before increment:3

sum after increment:6

sum before increment:6

sum after increment:10

output of enough function is:4

You might also like