0% found this document useful (0 votes)
2 views12 pages

Wa0038.

The document contains multiple C++ code snippets demonstrating various programming concepts such as classes, inheritance, operator overloading, and function overloading. Each code example illustrates different functionalities like entering and displaying data for cars and cricketers, managing birthdays, and performing arithmetic operations on complex numbers. The examples highlight the use of constructors, member functions, and polymorphism in C++.

Uploaded by

Manthan Bhavsar
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)
2 views12 pages

Wa0038.

The document contains multiple C++ code snippets demonstrating various programming concepts such as classes, inheritance, operator overloading, and function overloading. Each code example illustrates different functionalities like entering and displaying data for cars and cricketers, managing birthdays, and performing arithmetic operations on complex numbers. The examples highlight the use of constructors, member functions, and polymorphism in C++.

Uploaded by

Manthan Bhavsar
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/ 12

#include<iostream.

h>
#include<conio.h>
class car
{
protected:
char car_type[20];
public:
void enterCarType(){
cout<<“Enter the car type:- ”;
cin>>car_type;
}
void info(){
cout<<\nCar type: "<<car_type;
}
};
class brand:public car
{
protected:
char brand_name[20];
int speed:
public:
void enterBrandandSpeed()
{
cout<<“\n Enter brand of car and speed of car:- ”
cin>>brand_name>>speed;
}
};
class model :public brand
{
char modelName[20];
int price;
public:
void EnterInfo(){
cout<<"\nEnter the model name and price";
cin>>modelName>>price;
}
void infocar(){
cout<<"\nBrand name: "<<brand_name<<"\nSpeed: "<<speed:
cout<<"'\nModel name:"<<modelName<<"\nPrice:"<<price:
}
void main(){
clrscr();
car C;
C.enterCarType();
C.info();
model M;
M.enterBrandandSpeed();
M.EnterInfo();
M.infocar();
};
getch();
}

Output
#include <iostream>
using namespace std;
class cricketer
{
char c_name[20];
int c_score;
public:
void enter1()
{
cout<<"\n Enter cricketer name and score:";
cin>>c_name>>c_score;
}
void display1(){
cout<<"\n Cricketer's name: "<<c_name;
cout<<"\n Crickate's score: "<<c_score;
}
};
class bowler:public virtual cricketer
{
char b_name[20];
int wickets;
public:
void enter2(){
cout<<"\n Enter bowler's name and no. of wickects taken:";
cin>>b_name>>wickets;
}
void display2(){
cout<<"\n Bowler's name:"<<b_name;
cout<<"\n No.of wickets taken: "<<wickets;
}
};

class batsman:public virtual cricketer{


int runs;
char bt_name[20];
public:
void enter3(){
cout<<"\n Enter batsman's name and no. of runs made: ";
cin>>bt_name>>runs;
}
void display3(){
cout<<"\n Batsman's name:"<<bt_name;
cout<<"\n No. of runs made:"<<runs;
}
};

class allrounder:public bowler, public batsman


{
int a_score;
char a_name[20];
public:
void enter4(){
cout<<"\n Enter allrounder name and score : ";
cin>>a_name>>a_score;
}
void display4(){
cout<<"\nallrounder name: "<<a_name;
cout<<"\nallrounder score: "<<a_score;
}
};
int main()
{
allrounder n;
n.enter1();
n.enter2();
n.enter3();
n.enter4();
n.display1();
n.display2();
n.display3();
n.display4();
return 0;
}

Output
#include <iostream>
using namespace std;
class birthday{
int d,m,y;
public:
void getdata(){
cout<<"\nEnter your birthdate";
cin>>d>>m>>y;}
void putdata(){
cout<<"\nyour birthdate is: "<<d<<"/"<<m<<'/'<<y; }
};
int main(){
int i;
birthday B[5],*ptr;
ptr = & B[0];
for(i=0;i<5;i++){
ptr->getdata(); ptr++;}
ptr=& B[0];
for(i=0;i<5;i++){
ptr->putdata(); ptr++; }
return 0;}
Output

#include <iostream>
using namespace std;
class BaseClass {
public:
int var_base;
void display()
{
cout << "Displaying Base class"
<< " variable var_base: " << var_base << endl;
}
};
class DerivedClass : public BaseClass {
public:
int var_derived;
void display()
{
cout << "Displaying Base class"
<< "variable var_base: " << var_base << endl;
cout << "Displaying Derived "
<< " class variable var_derived: "
<< var_derived << endl;
}
};
int main()
{
BaseClass* base_class_pointer;
BaseClass obj_base;
DerivedClass obj_derived;

base_class_pointer = &obj_derived;

base_class_pointer->var_base = 34;

base_class_pointer->display();

base_class_pointer->var_base = 3400;
base_class_pointer->display();

DerivedClass* derived_class_pointer;
derived_class_pointer = &obj_derived;
derived_class_pointer->var_base = 9448;
derived_class_pointer->var_derived = 98;
derived_class_pointer->display();
return 0;

Output
#include <iostream>

using namespace std;


class Number {
private:
int value;

public:
Number() : value(0) {}

Number(int val) : value(val) {}

void display() {
cout << "Value: " << value << endl;
}

Number operator-() {
Number temp;
temp.value = -value;
return temp;
}
};

int main() {
Number num1(10);
Number num2;

cout << "Original value of num1:" <<endl;


num1.display();

num2 = -num1;
std::cout << "Value after unary operator overloading:" << std::endl;
num2.display();

return 0;
}
Output

#include <iostream>
using namespace std;

class Complex_num
{
int x, y;
public:
void inp()
{
cout << " Input two complex number: " << endl;
cin >> x >> y;
}

Complex_num operator + (Complex_num obj)


{
Complex_num A;
A.x = x + obj.x;
A.y = y + obj.y;
return (A);
}

Complex_num operator - (Complex_num obj)


{
Complex_num A;
A.x = x - obj.x;
A.y = y - obj.y;
return (A);
}

void print()
{
cout << x << " + " << y << "i" << "\n";
}

void print2()
{
cout << x << " - " << y << "i" << "\n";
}

};

int main ()
{
Complex_num x1, y1, sum, sub;
x1.inp();
y1.inp();
sum = x1 + y1;
sub = x1 - y1;
cout << "\n Entered values are: \n";
cout << " \t";
x1.print();
cout << " \t";
y1.print();
cout << "\n The addition of two complex (real and imaginary) numbers: ";
sum.print();
cout << "\n The subtraction of two complex (real and imaginary) numbers: ";
sub.print2();
return 0;
}
Output
#include <iostream>
using namespace std;
void add(int a, int b)
{
cout << "sum = " << (a + b);
}
void add(double a, double b)
{
cout << endl << "sum = " << (a + b);
}
int main()
{
add(1, 2);
add(3.3, 7.2);

return 0;
}

Output

You might also like