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

C++ 1

This document contains examples of different types of constructors in C++ including default, parameterized, and copy constructors. It also provides examples of inheritance including single, multilevel, multiple, and hybrid inheritance.

Uploaded by

Yogesh Tripathi
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)
45 views

C++ 1

This document contains examples of different types of constructors in C++ including default, parameterized, and copy constructors. It also provides examples of inheritance including single, multilevel, multiple, and hybrid inheritance.

Uploaded by

Yogesh Tripathi
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/ 5

Default Constructor Employee e1 =Employee(101, "Sonoo",

890000); //creating an object of Employee


#include <iostream> Employee e2=Employee(102, "Nakul",
using namespace std; 59000);
class Employee e1.display();
{ e2.display();
public: return 0;
Employee() }
{
cout<<"Default Constructor Invoked Copy constructor
"<<endl;
} #include <iostream>
}; using namespace std;
int main(void) class A
{ {
Employee e1; //creating an object of Empl public:
oyee int x;
Employee e2; A(int a) // parameterized constru
return 0; ctor.
} {
x=a;
Parametrized Constructor }
A(A &i) // copy constructor
#include <iostream> {
using namespace std; x = i.x;
class Employee { }
public: };
int id;//data member (also instance int main()
variable) {
string name;//data member(also instance A a1(20); // Calling the parameteri
variable) zed constructor.
float salary; A a2(a1); // Calling the copy cons
Employee(int i, string n, float s) tructor.
{ cout<<a2.x;
id = i; return 0;
name = n; }
salary = s;
} C++ Constructor and destructor
void display()
{ #include <iostream>
cout<<id<<" "<<name<<" using namespace std;
"<<salary<<endl; class Employee
} {
}; public:
int main(void) { Employee()
{
cout<<"Constructor Invoked"<<endl };
; class Dog: public Animal
} {
~Employee() public:
{ void bark(){
cout<<"Destructor Invoked"<<endl; cout<<"Barking...";
}
} };
}; int main(void) {
int main(void) Dog d1;
{ d1.eat();
Employee e1; //creating an object of Empl d1.bark();
oyee return 0;
Employee e2; //creating an object of Empl }
oyee
return 0; Example.
}
#include <iostream>
C++ Single Level Inheritance Example: using namespace std;
class A
#include <iostream> {
using namespace std; int a = 4;
class Account { int b = 5;
public: public:
float salary = 60000; int mul()
}; {
class Programmer: public Account { int c = a*b;
public: return c;
float bonus = 5000; }
}; };
int main(void) {
Programmer p1; class B : private A
cout<<"Salary: "<<p1.salary<<endl; {
cout<<"Bonus: "<<p1.bonus<<endl; public:
return 0; void display()
} {
int result = mul();
C++ Single Level Inheritance Example: std::cout <<"Multiplication of a and b i
s : "<<result<< std::endl;
#include <iostream> }
using namespace std; };
class Animal { int main()
public: {
void eat() { B b;
cout<<"Eating..."<<endl; b.display();
}
return 0; {
} a = n;
}
Example of multi level inheritance in };
C++.
class B
#include <iostream> {
using namespace std; protected:
class Animal { int b;
public: public:
void eat() { void get_b(int n)
cout<<"Eating..."<<endl; {
} b = n;
}; }
class Dog: public Animal };
{ class C : public A,public B
public: {
void bark(){ public:
cout<<"Barking..."<<endl; void display()
} {
}; std::cout << "The value of a is : " <<a<
class BabyDog: public Dog < std::endl;
{ std::cout << "The value of b is : " <<b<
public: < std::endl;
void weep() { cout<<"Addition of a and b is : "<<a+b
cout<<"Weeping..."; ;
} }
}; };
int main(void) { int main()
BabyDog d1; {
d1.eat(); C c;
d1.bark(); c.get_a(10);
d1.weep(); c.get_b(20);
return 0; c.display();
}
return 0;
Example of multiple inheritance. }

#include <iostream> Example of Hybrid Inheritance


using namespace std; :
class A #include <iostream>
{ using namespace std;
protected: class A
int a; {
public: protected:
void get_a(int n) int a;
public: }
void get_a() };
{ int main()
std::cout << "Enter the value of 'a' : " << {
std::endl; D d;
cin>>a; d.mul();
} return 0;
}; }

class B : public A Example of Hierarchical inheritance:


{
protected: #include <iostream>
int b; using namespace std;
public: class Shape // Declaration of base
void get_b() class.
{ {
std::cout << "Enter the value of 'b' : " < public:
< std::endl; int a;
cin>>b; int b;
} void get_data(int n,int m)
}; {
class C a= n;
{ b = m;
protected: }
int c; };
public: class Rectangle : public Shape // inheriting
void get_c() Shape class
{ {
std::cout << "Enter the value of c is : " public:
<< std::endl; int rect_area()
cin>>c; {
} int result = a*b;
}; return result;
}
class D : public B, public C };
{ class Triangle : public Shape // inheriting
protected: Shape class
int d; {
public: public:
void mul() int triangle_area()
{ {
get_a(); float result = 0.5*a*b;
get_b(); return result;
get_c(); }
std::cout << "Multiplication of a,b,c is };
: " <<a*b*c<< std::endl; int main()
{
Rectangle r;
Triangle t;
int length,breadth,base,height;
std::cout << "Enter the length and breadth
of a rectangle: " << std::endl;
cin>>length>>breadth;
r.get_data(length,breadth);
int m = r.rect_area();
std::cout << "Area of the rectangle is : " <
<m<< std::endl;
std::cout << "Enter the base and height of
the triangle: " << std::endl;
cin>>base>>height;
t.get_data(base,height);
float n = t.triangle_area();
std::cout <<"Area of the triangle is : " <<
n<<std::endl;
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy