CPP Journal
CPP Journal
1. Simple if Statement:
#include<iostream.h>
#include<conio.h>
Void main()
Clrscr();
int age=20;
if(age>=18)
getch();
O/P:-
#include<iostream.h>
#include<conio.h>
void main()
int number;
clrscr();
cin>>“%d”, &number;
if(number % 2 == 0)
else
getch();
O/P:-
Enter an integer: 10
10 is even.
3.Nested if … else statement:
#include<iostream.h>
#include<conio.h>
void main()
cout<<“Result: %d = %d”,number1,number2;
getch();
}
O/P:-
23
Result: 12 < 23
Q3) Write a program to create a class and creating an object.
#include <iostream.h>
#include <string.h>
class MyClass
{
public:
int myNum;
string myString;
};
int main()
{
MyClass myObj;
myObj.myNum = 15;
myObj.myString = "Some text";
cout << myObj.myNum << "\n";
cout << myObj.myString;
return 0;
}
O/P:-
50
Q4) Illustrating different Access Specifiers.
#include<iostream.h>
#include<conio.h>
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;
}
O/P:-
Radius is: 5.5
Area is: 94.985
Q5) Write a oop program to demonstrate static data member
#include <iostream.h>
#include <conio.h>
class A
{
public:
A()
{
cout << "A's Constructor Called " << endl;
}
};
class B
{
static A a;
public:
B()
{
cout << "B's Constructor Called " << endl;
}
};
int main()
{
B b;
return 0;
}
O/P:-
B's Constructor Called
Q6) Demostrate arguments to the function.
#include <iostream.h>
#include <conio.h>
void myFunction(string fname)
{
cout << fname << " Refsnes\n";
}
int main()
{
myFunction("Liam");
myFunction("Jenny");
myFunction("Anja");
return 0;
}
O/P:-
Liam Refsnes
Jenny Refsnes
Anja Refsnes*/
Q7) Illustrate inline function.
#include <iostream.h>
#include <conio.h>
inline int cube(int s)
{
return s * s * s;
}
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;
}
O/P:-
The cube of 3 is: 27
Q8) Define member function-outside the class using Scope
Resolution Operator
#include <iostream.h>
#include <conio.h>
class Operate
{
public:
void fun();
};
void Operate::fun()
{
cout << " It is the member function of the class. ";
}
int main ()
{
Operate op;
op.fun();
return 0;
}
O/P:-
It is the member function of the class.
Q9) Illustrating friend class and friend function.
#include <iostream.h>
#include<conio.h>
class GFG
{
private:
int private_variable;
protected:
int protected_variable;
public:
GFG()
{
private_variable = 10;
protected_variable = 99;
}
// friend class declaration
friend class F;
};
class F
{
public:
void display(GFG& t)
{
cout << "The value of Private Variable = "
<< t.private_variable << endl;
cout << "The value of Protected Variable = "
<< t.protected_variable;
}
};
int main()
{
GFG g;
11
F fri;
fri.display(g);
return 0;
}
O/P:-
The value of Private Variable = 10
The value of Protected Variable = 99
Q10) Create constructor- default & parameterized.
1) Default:-
#include <iostream.h>
class Prog
public :
cout<<“Default Constructor.”<<endl;
};
int main()
return 0;
O/P:-
1.Default Constructor.
2.Default Constructor.
3.Default Constructor.
2) Parameterized:-
#include <iostream.h>
class Prog
private:
int a, b, c;
public :
a = x;
b = y;
c = a + b;
void display()
};
int main()
return 0;
O/P:-
Addition of 15 and 16 is 31
Q11) Destructor
#include <iostream.h>
using namespace std;
class Prog
{
int a;
public:
Prog(int x) // Constructor
{
a = x;
cout<<“Constructor is created.”<<endl;
}
~Prog() //Destructor
{
cout<<“Constructor is deleted.”<<endl;
}
void show()
{
cout<<“Value of a : “<<a<<endl;
}
};
int main()
{
Prog a(5);
a.show();
return 0;
}
O/P:-
Constructor is created.
Value of a : 5
Constructor is deleted.
Q12) Dynamic initialization of Object.
#include <iostream>
#include<conio.h>
class MyClass {
public:
MyClass() {
std::cout << "Constructor called" << std::endl;
}
~MyClass() {
std::cout << "Destructor called" << std::endl;
}
void display() {
std::cout << "Hello from MyClass!" << std::endl;
}
};
int main() {
MyClass *ptr = new MyClass();
ptr->display();
delete ptr;
return 0;
}
O/P:-
Constructor called
Hello from MyClass!
Destructor called
Q13) Illustrating inheritance- Single.
Single Inheritance -
#include <iostream.h>
#include <conio.h>
class Account
{
public:
float salary = 60000;
};
class Programmer: public Account
{
public:
float bonus = 5000;
};
int main(void)
{
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
O/P:-
Salary: 60000
Bonus: 5000
Q14) Perform a static polymorphism.
Static Polymorphism:-
#include<iostream.h>
#include<iomanip.h>
class Add
public:
return n1+n2;
return n1+n2+n3;
};
int main()
Add p;
return 0;
}
O/P:-
Sum: 66
Total: 130
Virtual Function:-
#include <iostream.h>
#include <conio.h>
class base
{
public:
virtual void print() { cout << "print base class\n";
}
void show() { cout << "show base class\n"; }
};
class derived : public base
{
public:
void print()
{
cout << "print derived class\n";
}
void show()
{
cout << "show derived class\n";
}
};
int main()
{ base* bptr;
derived d;
bptr = &d;
bptr->print();
bptr->show();
return 0;
}
O/P:-
print derived class
show base class
Pure Function:-
#include <iostream.h>
#include <conio.h>
class Base
{
int x;
public:
virtual void fun() = 0;
int getX()
{
return x;
}
};
class Derived : public Base
{
int y;
public:
void fun()
{
cout << "fun() called";
}
};
int main(void)
{
Derived d;
d.fun();
return 0;
}
O/P:-
fun() called