Professional Documents
Culture Documents
C++ Practical
C++ Practical
USING C++
Name : Dipesh
Roll No : 1220191011092
Registration No : 2211391091
INDEX
S.No Topic Page No
Write a program to check whether
a number is prime or not
#include <iostream>
using namespace std;
int main() {
int i,j,flag=0;
for(j=2;j<i;j++){
if(i%j==0){
flag=1;
break;
}
}
if(flag==0)
{
cout<<"Entered number is prime";
}else{
cout<<"Entered number is not prime";
}
return 0;
}
OUTPUT :
Write a program to execute
enumeration datatype
#include <iostream>
using namespace std;
int main() {
enum Gender { Male, Female };
switch (gender) {
case Male:
cout << "Gender is Male";
break;
case Female:
cout << "Gender is Female";
break;
default:
cout << "Value can be Male or Female";
}
return 0;
}
OUTPUT :
Write a program to implement
class and object
#include <iostream>
using namespace std;
class User {
public:
string username;
void printname() { cout << "username is:" << username;
} };
int main()
{
User obj1;
obj1.username = "Daksh";
obj1.printname();
return 0;
}
OUTPUT :
Write a program to implement
constructor and destructor
#include <iostream>
using namespace std;
class Test {
public:
int main(){
Test t, t1, t2;
return 0;
}
OUTPUT :
Write a program to implement
copy constructor
#include <iostream>
using namespace std;
class A {
public:
int x;
A(int a){
x=a; }
A(A &i){
x = i.x; } };
int main() {
A a1(20);
A a2(a1);
cout<<a2.x;
return 0;
}
OUTPUT :
Write a program to implement
constructor overloading
#include <iostream>
using namespace std;
class Person {
private:
int age;
public:
Person() {
age = 20;
}
Person(int a) {
age = a;
}
int getAge() {
return age;
}};
int main() {
Person person1, person2(45);
return 0;}
OUTPUT :
Write a program to implement
function overloading
#include <iostream>
using namespace std;
int main() {
int myNum1 = plusFunc(8, 5);
double myNum2 = plusFunc(4.3, 6.26);
OUTPUT :
Write a program to implement
hierarchical inheritance
#include<iostream>
using namespace std;
class A {
public:
void show_A() {
cout<<"class A"<<endl;
} };
class B : public A {
public:
void show_B() {
cout<<"class B"<<endl;
} };
class C : public A{
public:
void show_C() {
cout<<"class C"<<endl;
} };
int main() {
B b;
cout<<"calling from B: "<<endl;
b.show_B();
b.show_A();
C c;
cout<<"calling from C: "<<endl;
c.show_C();
c.show_A();
return 0;
}
OUTPUT :
Write a program to implement
multilevel inheritance.
#include <iostream>
using namespace std;
class MyClass {
public:
void myFunction() {
cout << "Some content in parent class." ;
}};
int main() {
MyGrandChild myObj;
myObj.myFunction();
return 0;
}
OUTPUT :
Write a program to implement
function overriding.
#include <iostream>
using namespace std;
class Parent {
public:
void Print() {
cout << "Base Function" << endl;
} };
int main(){
Child Child_Derived;
Child_Derived.Print();
return 0;
}
OUTPUT :
Write a program to access static
member variables and function
#include <iostream>
using namespace std;
class Student {
public:
static int total;
Student() { total += 1; } };
int Student::total = 0;
int main() {
Student s1;
cout << "Number of students:" << s1.total << endl;
Student s2;
cout << "Number of students:" << s2.total << endl;
Student s3;
cout << "Number of students:" << s3.total << endl;
return 0;
}
OUTPUT :
Write a program to implement
exception handling with multiple catch
#include <iostream>
using namespace std;
int main() {
try {
float x,y;
cout << "Enter a number for denominator: ";
cin >> x;
if (x == 0) {
throw invalid_argument("Invalid input.
denominator cannot be zero."); }
y = 10/ x;
cout << "Result: " << y <<endl;
}
return 0;
}
OUTPUT :
Write a program to implement
implement new and delete operator
#include <iostream>
using namespace std;
int main() {
delete[] arr;
return 0;
}
OUTPUT :