Recursion.
Q1. /*Write a program to sum of N number using recursion*/
#include<iostream>
using namespace std;
int nsum(int n){
if (n==0){//base condintion
return 0;
int rec=n+nsum(n-1);//recursive condition
return rec;
int main(){
int n;
cout<<"Enter the number=";
cin>>n;
int sum=nsum(n);
cout<<"sum="<<sum;
return 0;
Q2. Write a program to find factorial number using recursion.
#include<iostream>
using namespace std;
int nfact (int factorial){
if(factorial==1){
return 1;
int rac=factorial*nfact(factorial-1);
return rac;
int main(){
int factorial;
cout<<"Any Naturial number=";
cin>>factorial;
int fact=nfact(factorial);
cout<<"factorial="<<fact;
return 0;
Q3.write a program to Fibonnaci series using recursion.
#include<iostream>
using namespace std;
void printFibonacci(int n){
static int n1=0, n2=1, n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
cout<<n3<<" ";
printFibonacci(n-1);
}
int main(){
int n;
cout<<"Enter the number of elements: ";
cin>>n;
cout<<"Fibonacci Series: ";
cout<<"0 "<<"1 ";
printFibonacci(n-2);
return 0;
Object Oriented Programming in C++ (OOPS).
Q1.write a program to print book details Ex. Title, One Author, and
price.
#include<iostream>
using namespace std;
class Book{
public:
string title;
string author;
float price;
};
int main(){
Book bookdetail;
bookdetail.title="Ramayan";
bookdetail.author ="Walmiki";
bookdetail.price=452010;
cout<<bookdetail.title<<endl;
cout<<bookdetail.author<<endl;
cout<<bookdetail.price<<endl;
return 0;
Q2.write a program to make a calculator using class.
#include<iostream>
using namespace std;
class calculator{
private:
int x;
int y;
public:
void getdata(){
cout<<"Enter a first number:";
cin>>x;
cout<<"Enter a secomd number:";
cin>>y;
void display(){
cout<<"x="<< x <<endl;
cout<<"y="<<y<<endl;
cout<<"sum="<<sum()<<endl;
cout<<"Difference="<<diff()<<endl;
cout<<"Multiplication="<<mul()<<endl;
cout<<"Division="<<div()<<endl;
int sum(){
return(x+y);
int diff(){
return(x-y);
int mul(){
return(x*y);
int div(){
return(x/y);
};
int main(){
calculator Cl;
Cl.getdata();
Cl.display();
Cl.sum();
Cl.diff();
Cl.mul();
Cl.div();
}
Q3. Write a program to car detail using class.
#include<iostream>
using namespace std;
class Myclass{
public:
string brand ;
string modle;
int year;
};
int main(){
Myclass myobj;
myobj.brand = "BMW";
myobj.modle= "X7";
myobj.year =1987;
Myclass myobj1;
myobj1.brand="Audi";
myobj1.modle="X45";
myobj1.year=1998;
cout<<myobj.brand<<endl;
cout<<myobj.modle<<endl;
cout<<myobj.year<<endl;
cout<<myobj1.brand<<endl;
cout<<myobj1.modle<<endl;
cout<<myobj1.year<<endl;
Q3.write a program to print day month and year.
#include<iostream>
using namespace std;
class date{
public:
int day;
int month;
int year;
};
int main(){
date dl;
dl.day=17;
dl.month= 9;
dl.year=2004;
cout<<dl.day<<"/"<<dl.month<<"/"<<dl.year<<endl;
return 0;
}
Member Function
(i) inside function (.)
#include <iostream>
using namespace std;
class MyClass {
public:
void myMethod() {
cout << "Hello World!";
}
};
int main() {
MyClass myObj;
myObj.myMethod();
return 0;
}
(ii) Outside function (::)
#include <iostream>
using namespace std;
class MyClass {
public:
void memberFunctionOutside();
};
void MyClass::memberFunctionOutside() {
cout << "Outside member function ." << endl;
}
int main() {
MyClass obj;
obj.memberFunctionOutside();
return 0;
}
Constructor.
(i) Default constructor
#include <iostream>
using namespace std;
class Person{
private:
string name;
int age;
public:
Person()
{
cout<<"Default constructor :"<<endl;
name = "Anand";
age = 20;
}
void display()
{
cout<<"Name of current object: "<<name<<endl;
cout<<"Age of current object: "<<age<<endl;
}
};
int main()
{
Person obj;
obj.display();
return 0;
}
(ii) Parameterized Constructor.
#include <iostream>
using namespace std;
class Person{
private:
string name;
int age;
public:
Person(string person_name)
cout<<"Constructor to set name is called"<<endl;
name = person_name;
age = 12;
Person(int person_age)
cout<<"Constructor to set age is called"<<endl;
name = "Student";
age = person_age;
Person(string person_name, int person_age)
cout<<"Constructor for both name and age is called"<<endl;
name = person_name;
age = person_age;
void display()
cout<<"Name of current object: "<<name<<endl;
cout<<"Age of current object: "<<age<<endl;
cout<<endl;
};
int main()
Person obj1("First person");
obj1.display();
Person obj2(25);
obj2.display();
Person obj3("Second person",15);
obj3.display();
return 0;
(iii) Copy constructor
#include <iostream>
using namespace std;
class Person{
private:
string name;
int age;
public:
Person(string person_name, int person_age)
cout<<"Parametrized Constructor"<<endl;
name = person_name;
age = person_age;
Person(const Person& obj)
cout<<"Copy constructor "<<endl;
name = obj.name;
age = obj.age;
void display()
cout<<"Student name : "<<name<<endl;
cout<<"Age of student : "<<age<<endl;
cout<<endl;
};
int main()
Person obj1("Anand ",20);
obj1.display();
Person obj2(obj1);
obj2.display();
return 0;
(iv) Dynamic constructor .
#include <iostream>
using namespace std;
class Person{
private:
int* age;
public:
Person(int* person_age)
cout<<"Dynamic Constructor:"<<endl;
age = new int;
age = person_age;
void display()
cout<<"Age of current object: "<<*age<<endl;
cout<<endl;
};
int main()
int age = 25;
Person obj1(&age);
obj1.display();
return 0;
Destructor
#include <iostream>
using namespace std;
class class_name{
private:
int a,b;
public:
class_name(int aa, int bb)
cout<<"Constructor value :"<<endl;
a = aa;
b = bb;
cout<<"Value of a: "<<a<<endl;
cout<<"Value of b: "<<b<<endl;
cout<<endl;
~class_name()
cout<<"Destructor value :"<<endl;
cout<<"Value of a: "<<a<<endl;
cout<<"Value of b: "<<b<<endl;
};
int main()
class_name obj(5,6);
return 0;
Inheritance.
Q. Write a program on Single Inheritance in class .
#include<iostream>
using namespace std;
class Animal{
public:
Animal(){
cout<<"This is animal ."<<endl;
};
class dog : public Animal{
public:
dog(){
cout <<"Dog always barks .";
};
int main(){
dog d1;
return 0;
Q. Write a program on Multiple Inheritance in class .
#include<iostream>
using namespace std ;
class Animal{
public:
Animal(){
cout<<"This is Animal ."<<endl ; }
};
class Mammal{
public:
Mammal(){
cout<<"This is Mammal ."<<endl;
};
class dog : public Mammal , public Animal{
public:
dog(){
cout<<"Dog always barks . "<<endl;
};
int main(){
dog d1;
return 0;
Write a program on Multilevel Inheritance in class .
#include<iostream>
using namespace std ;
class wood{
public:
wood(){
cout<<"Wood are made from tree ."<<endl;
};
class furniture : public wood{
public:
furniture(){
cout<<"Furniture are made from Wood ."<<endl;
};
class table : public furniture{
public:
table(){
cout<<"Table is type of furniture . "<<endl;
};
int main(){
table t1;
return 0;
Q. Write a program on Hierarchical Inheritance in class .
#include<iostream>
using namespace std ;
class Animal {
public:
Animal() {
cout << "Animal is eating ." << endl;
};
class Dog : public Animal {
public:
Dog() {
cout << "Dog is barking ." << endl;
};
class Cat : public Animal {
public:
Cat() {
cout << "Cat is meowing ." << endl;
};
int main(){
Cat c1;
Dog d1;
return 0;
}
Q. Write a program on Hybrid Inheritance in class .
#include<iostream>
using namespace std;
class grandparent{
public:
grandparent(){
cout<<"Grandparents love their child and grandchild."<<endl;
};
class parent : virtual public grandparent{
public:
parent(){
cout<<"Parent take care of their child"<<endl;
};
class child: public parent, virtual public grandparent{
public:
child(){
cout<<"Child should give respect to his parents and grandparents."<<endl;
};
int main(){
child obj;
return 0;