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.
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 ratings0% 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.
#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; }
(Ebook) The Garbage Collection Handbook. The Art of Automatic Memory Management, 2nd by Richard Jones, Antony Hosking, Eliot Moss ISBN 9781032218038, 9781032231785, 1032218037, 1032231785 All Chapters Instant Download