0% found this document useful (0 votes)
18 views

Final Journal C++.ABC

The document contains the code submissions for various C++ programming assignments. It includes programs demonstrating simple concepts like input/output, control structures, classes and objects, inheritance and polymorphism. Different programs are provided to illustrate concepts like default constructor, parameterized constructor, copy constructor, static member functions, access specifiers, and more.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Final Journal C++.ABC

The document contains the code submissions for various C++ programming assignments. It includes programs demonstrating simple concepts like input/output, control structures, classes and objects, inheritance and polymorphism. Different programs are provided to illustrate concepts like default constructor, parameterized constructor, copy constructor, static member functions, access specifiers, and more.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Roll no :

Assignment no :01
Assignment Name: Write a CPP program to calculate
Simple Interest.
Input:
#include<iostream>
using namespace std;
int main()
{
int p,t,r,SI;
cout<<"Enter the amount: ";
cin>>p;
cout<<"Enter the time period: ";
cin>>t;
cout<<"Enter the rate of interest: ";
cin>>r;
SI=(p*t*r)/100;
cout<<"Simple Interest: "<<SI<<endl;
return 0;
}
Output:
Enter the amount: 5000
Enter the time period: 3
Enter the rate of interest: 1000
Simple Interest: 150000
Roll no :
Assignment no :02
Assignment Name: Write a CPP program to Illustrating control structures.

1. If else statement:
Input:
#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"Enter any number: ";
cin>>a;
if(a>0)
{
cout<<"Number is positive";
}
else
{
cout<<"Number is negative";
}
return 0;
}
Output:
Enter any number: 7
Number is positive

2. For statement:
Input:
#include<iostream>
using namespace std;
int main()
{
int i=1;
for(i=1;i<=10;i++)
{
cout<<"All the Best!"<<endl;
}
return 0;
}
Output:
All the Best!
All the Best!
All the Best!
All the Best!
All the Best!
All the Best!
All the Best!
All the Best!
All the Best!
All the Best!

3.Continue statement:
Input:
#include<iostream>
using namespace std;
int main()
{
int i;
for(i=1;i<=10;i++)
{
if(i==6)
{
continue;
}
cout<<i<<endl;
}
return 0;
}
Output:
1
2
3
4
5
7
8
9
10
Roll no :
Assignment no :03
Assignment Name: Write a CPP program to create class and object.

Input:
#include<iostream>
using namespace std;
class addition
{
private:
int a,b,c;
public:
void input()
{
cout<<"Enter a & b: ";
cin>>a>>b;
}
void output()
{
c=a+b;
cout<<"Add is: "<<c;
}
};
int main()
{
addition k;
k.input();
k.output();
return 0;
}
Output:
Enter a & b: 4 7
Add is: 11
Roll no :
Assignment no :04
Assignment Name: Write a CPP program to Illustrating different access
Specifiers.

1.Public Specifiers:
Input:
#include<iostream>
using namespace std;
class Circle
{
public:
double radius;
double compute_area()
{
return 3.14*radius*radius;
}
};
int main()
{
Circle obj;
obj.radius = 5.5;
cout<<"Radius is: "<<obj.radius<<"\n";
cout<<"Area is: "<<obj.compute_area();
return 0;
}
Output:
Radius is: 5.5
Area is: 94.98

2.Private Program:
Input:
#include<iostream>
using namespace std;
class Circle
{
private:
double radius;
public:
void compute-area(double r)
{
radius = r;
double area = 3.14*radius*radius;
cout<<"Radius is: "<<radius<<endl;
cout<<"Area is: "<<area;
}
};
int main()
{
Circle obj;
obj.compute-area(1.5);
return 0;
}
Output:
Radius is: 1.5
Area is: 7.065

3.Protected Program:
Input:
#include<iostream>
using namespace std;
class parent
{
protected:
int id_protected;
};
class child:public parent
{
public:
void satld(int id)
{
id_protected = id;
}
void displayld()
{
cout<<"id_protected is:"<<id_protected<<endl;
}
};
int main()
{
child obj1;
obj1.satld(81);
obj1.displayld();
return 0;
}
Output:
id_protected is:81
Roll no :
Assignment no :05
Assignment Name: Write a CPP program to demonstrate static
Data member.

Input:
#include<iostream>
using namespace std;
class item
{
private:
int number;
static int count;
public:
void getdata(int a)
{
number=a;
count++;
}
void getcount()
{
cout<<"Count is: "<<count<<endl;
}
};
int item :: count;
int main()
{
item k1,k2;
cout<<"Before reading ";
k1.getcount();
k2.getcount();
k1.getdata(100);
k2.getdata(200);
cout<<"After reading ";
k1.getcount();
k2.getcount();

return 0;
}
Output:
Before reading Count is: 0
Count is: 0
After reading Count is: 2
Count is: 2
Roll no :
Assignment no :06
Assignment Name: Write a CPP program to Demonstrate arguments to the
Functions.

Input:
#include<iostream>
using namespace std;
int myfunction(string name)
{
cout<<name;
}
int main()
{
myfunction("Afnan");
return 0;
}
Output:
Afnan
Roll no :
Assignment no : 07
Assignment Name: Write a CPP program to Illustrating inline function.

Input:

#include<iostream>
using namespace std;
inline int add(int a,int b)
{
return (a+b);
}
inline int sub(int m,int n)
{
return (m-n);
}
int main()
{
int x,y;
cout<<"Enter x & y: ";
cin>>x>>y;
cout<<add(x,y);
cout<<"\n";
cout<<sub(x,y);
return 0;
}
Output:
Enter x & y: 7 4
11
3
Roll no :
Assignment no :08
Assignment Name: Write a CPP program to define member function outside
the class using scope resolution operator.

Input:
#include<iostream>
using namespace std;
class circle
{
private:
float a,r;
public:
void accept();
void display();
};
void circle::accept()
{
cout<<"Enter radius: ";
cin>>r;
}
void circle::display()
{
a=3.14*r*r;
cout<<"Area is: "<<a;
}
int main()
{
circle k;
k.accept();
k.display();
return 0;
}
Output:
Enter radius: 5
Area is: 78.5
Roll no :
Assignment no :09
Assignment Name: Write a CPP program to demonstrate types of
Inheritances.

1. Single Inheritance:

Input:
#include<iostream>
using namespace std;
class Base
{
public:
int roll_no;
char name[30];
public:
void input()
{
cout<<"\n Enter Roll No:";
cin>>roll_no;
cout<<"\n Enter Name:";
cin>>name;
}
};
class Derived:public Base
{
public:
void output()
{
cout<<"\n My Roll No is:"<<roll_no;
cout<<"\n My Name is:"<<name;
}
};
int main()
{
Derived D;
D.input();
D.output();
return 0;
}
Output:
Enter Roll No: 45

Enter Name: Rohit


My Roll No is: 45
My Name is: Rohit

2. Multiple Inheritance:
Input:
#include<iostream>
using namespace std;
class Base
{
public:
int roll_no;
public:
void input()
{
cout<<"\n Enter Roll No:";
cin>>roll_no;
}
};
class Derived
{
public:
char name[30];
public:
void getdata()
{
cout<<"\n Enter Name:";
cin>>name;
}
};
class Multi_inheri:public Base,public Derived
{
public:
void output()
{
cout<<"\n My Roll No is:"<<roll_no;
cout<<"\n My Name is:"<<name;
}
};
int main()
{
Multi_inheri M;
M.input();
M.getdata();
M.output();
return 0;
}
Output:

Enter Roll No:18

Enter Name: Virat

My Roll No is:18
My Name is: Virat
3. Multilevel Inheritance:

Input:
#include<iostream>
using namespace std;
class Base
{
public:
int roll_no;
public:
void input()
{
cout<<"\n Enter Roll No:";
cin>>roll_no;
}
};
class Derived:public Base
{
public:
char name[30];
public:
void getdata()
{
cout<<"\n Enter Name:";
cin>>name;
}
};
class Multi_inheri:public Derived
{
public:
void output()
{
cout<<"\n My Roll No is:"<<roll_no;
cout<<"\n My Name is:"<<name;
}
};
int main()
{
Multi_inheri M;
M.input();
M.getdata();
M.output();
return 0;
}
Output:

Enter Roll No:7

Enter Name: Dhoni

My Roll No is:7
My Name is: Dhoni

4. Hierarchial Inheritance:

Input:
#include<iostream>
using namespace std;
class A
{
public:
int a,b;
public:
int getdata()
{
cout<<"\n Enter a and b: ";
cin>>a>>b;
}
};
class B:public A
{
public:
int product()
{
cout<<"\n Product= "<<a*b;
}
};
classC:public A
{
public:
int sum()
{
cout<<"\n Sum= "<<a+b;
}
};
int main()
{
B obj1;
C obj2;
obj1.getdata();
obj1.produt();
obj2.getdata();
obj2.sum();
return 0;
}
Output:
Enter a and b: 18 19
Product= 342
Enter a and b: 7 2
Sum=3
Roll no :
Assignment no :10
Assignment Name: Write a CPP program to create constructors(default,
parameterized, Copy a destructor).

1. Default Constructor:

Input:
#include<iostream>
using namespace std;
class sample
{
private:
int a,b;
public:
sample()
{
cout<<"Enter a & b: ";
cin>>a>>b;
}
int output()
{
cout<<"Value of a is: "<<a<<endl;
cout<<"Value of b is: "<<b;
}
};
int main()
{
sample k;
k.output();
return 0;
}
Output:
Enter a & b: 26 18
Value of a is: 26
Value of b is: 18
2. Parameterized Constructor:

Input:
#include<iostream>
using namespace std;
class sample
{
private:
int a,b;
public:
sample(int x,int y)
{
a=x;
b=y;
}
int output()
{
cout<<"Value of a is: "<<a<<endl;
cout<<"Value of b is "<<b<<endl;
}
};
int main()
{
sample z(12,34);
z.output();
return 0;
}

Output:
Value of a is: 12
Value of b is 34
3. Copy constructor:

Input:
#include<iostream>
using namespace std;
class sample
{
private:
int a,b;
public:
sample(int x,int y)
{
a=x;
b=y;
}
int output()
{
cout<<"Value of a is: "<<a<<endl;
cout<<"Value of a is: "<<b<<endl;
}
};
int main()
{
sample k(18,19);
sample m=k;
cout<<"Output of Parameterised constructor: "<<endl;
k.output();
cout<<"Output of Copy constructor: "<<endl;
m.output();
return 0;
}
Output:
Output of Parameterised constructor:
Value of a is: 18
Value of a is: 19
Output of Copy constructor:
Value of a is: 18
Value of a is: 19
4. Destructors:

Input:
#include<iostream>
using namespace std;
class sample
{
private:
int a,b;
public:
sample()
{
cout<<"Enter a & b: ";
cin>>a>>b;
}
~sample()
{
cout<<"Enter a & b: ";
cin>>a>>b;
}
int output()
{
cout<<"Value of a is: "<<a<<endl;
cout<<"Value of b is: "<<b<<endl;
}
};
int main()
{
sample s;
s.output();
return 0;
}
Output:
Enter a & b: 18 19
Value of a is: 18
Value of b is: 19
Enter a & b: 7 4

You might also like