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

CPP Journal

cpp simple project
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

CPP Journal

cpp simple project
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Index

Sr.No Description Date Sign


1
Write a simple program (Without
Class) to use of operations in C++.

2 Illustrating Control Structures.

3 Write a program to create a class and


creating an object.
4 Illustrating different Access Specifiers.

5 Write a oop program to demonstrate


static data member
6 Demostrate arguments to the function
7 Illustrate inline function.

8 Define member function-outside the


class using Scope Resolution Operator
9 Illustrating friend class and friend
function
10 Create constructor- default &
parameterized.
11 Destructor

12 Dynamic initialization of Object.

13 Illustrating inheritance- Single.

14 Perform a static and dynamic


polymorphism.
15 Demostrate virtual and pure function
Krantiagrani Dr. G. D. Bapu Lad Mahavidyalaya, Kundal.
Department of B.C.A
Year=2023-24
Roll No.: Name: ……………………………………..
Date: Class: BCA-II
Subject: C++ Sem: IV
Q1) Write a simple program (Without Class) to use of operations in
C++.
#include <iostream>
#include<conio.h>
int main()
{
int a = 8, b = 3;
cout << "a + b = " << (a + b) << endl;
cout << "a - b = " << (a - b) << endl;
cout << "a * b = " << (a * b) << endl;
cout << "a / b = " << (a / b) << endl;
cout << "a % b = " << (a % b) << endl;
return 0;
}
O/P:-
a + b = 11
a-b=5
a * b = 24
a/b=2
a%b=2
Q2) Illustrating Control Structures.

1. Simple if Statement:

#include<iostream.h>

#include<conio.h>

Void main()

Clrscr();

int age=20;

if(age>=18)

Cout<<”are you eligible for voting ”<<endl;

getch();

O/P:-

are you eligible for voting


2. The if…else statement:

#include<iostream.h>

#include<conio.h>

void main()

int number;

clrscr();

cout<<“\n Enter an integer: “;

cin>>“%d”, &number;

// True if the number is divisible by 2

if(number % 2 == 0)

cout<<“%d is even.”, number;

else

cout<<“%d is odd.”, number;

getch();

O/P:-

Enter an integer: 10

10 is even.
3.Nested if … else statement:

#include<iostream.h>

#include<conio.h>

void main()

int number1, number2;

cout<<“Enter two integers: “;

cin>>“%d %d”, &number1, &number2;

if(number1 == number2) //checks if two integers are equal.

cout<<“Result: %d = %d”,number1,number2;

else if (number1 > number2) //checks if number1 is greater than


number2.

cout<<“Result: %d > %d”, number1, number2;

else // if both test expression is false

cout<<“Result: %d < %d”,number1, number2;

getch();

}
O/P:-

Enter two integers: 12

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>

using namespace std;

class Prog

public :

Prog() // Default Constructor

cout<<“Default Constructor.”<<endl;

};

int main()

Prog s1, s2, s3;

return 0;

O/P:-

1.Default Constructor.
2.Default Constructor.
3.Default Constructor.
2) Parameterized:-

#include <iostream.h>

using namespace std;

class Prog

private:

int a, b, c;

public :

Prog(int x, int y) // Parameterized Constructor

a = x;

b = y;

c = a + b;

void display()

cout<<“Addition of “<<a<<” and “<<b<<” is “<<c;

};

int main()

Prog a(15, 16);


a.display();

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>

using namespace std;

class Add

public:

int sum(int n1, int n2)

return n1+n2;

int sum(int n1, int n2, int n3)

return n1+n2+n3;

};

int main()

Add p;

cout<<“Sum : “<<p.sum(22, 44)<<endl;

cout<<“Total: “<<p.sum(20, 50, 60);

return 0;
}

O/P:-

Sum: 66
Total: 130

Q15) Demostrate virtual and pure function.

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

You might also like