Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

OBJECT ORIENTED PROGRAMMING

USING C++

Name : Dipesh

Course : BCA 4th Semester

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;

cout << "write a number\n";


cin>> i;

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 };

Gender gender = Male;

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:

Test() { cout << "\n Constructor executed"; }

~Test() { cout << "\n Destructor executed"; }


};

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);

cout << "Person1 Age = " << person1.getAge() << endl;


cout << "Person2 Age = " << person2.getAge() << endl;

return 0;}
OUTPUT :
Write a program to implement
function overloading
#include <iostream>
using namespace std;

int plusFunc(int x, int y) {


return x + y;
}

double plusFunc(double x, double y) {


return x + y;
}

int main() {
int myNum1 = plusFunc(8, 5);
double myNum2 = plusFunc(4.3, 6.26);

cout << "Int: " << myNum1 << "\n";


cout << "Double: " << myNum2;
return 0;
}

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." ;
}};

class MyChild: public MyClass {


};

class MyGrandChild: public MyChild {


};

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;
} };

class Child : public Parent {


public:
void Print() {
cout << "Derived 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;
}

catch (const invalid_argument& e) {


cerr << e.what() <<endl;
}
catch (...) {
cerr << "An unknown error occurred." << endl;
}

return 0;
}
OUTPUT :
Write a program to implement
implement new and delete operator
#include <iostream>
using namespace std;

int main() {

int* arr = new int[5];

for (int i = 0; i < 5; ++i) {


arr[i] = i * 10;
}

cout << "Array elements: ";


for (int i = 0; i < 5; ++i) {
cout << arr[i] << " ";
}

delete[] arr;

return 0;
}

OUTPUT :

You might also like