“Konstruktor w CPP” Kod odpowiedzi

Klasy konstruktor w C.

//constructor
//carrys the same name of the class
//it is neither void nor return
//is called during the declaration of object

class Triangle {
private:
	float base;
	float height;
public:
	//constructor
	//Type 1(empty constructor,without parameters nor arguments)
	Triangle() {
		cout << "First constructor\n";
		height = base = 0;					//gives initial value to the class members
	}

	//Type 2(parameterized constructor)
	Triangle(float b, float h) {
		cout << "parameterized constructor\n";
		base = b;
		height = h;
	}
  
  	//default constructor
	Student(float b, float h = 5) {
		cout << "Default parameterized constructor\n";
		base = b;
		height = h;
    }

  //print function
  void print() {
		cout << "Base= " << getBase() << endl;
		cout << "Height= " << getHeight() << endl;
	}
};

int main() {
  // triangle will call empty Constructor
	Triangle triangle;
  	triangle.print();
  
  // triangle2 will call parameterized Constructor
	Triangle triangle2;
  	triangle2.print();
  
   // triangle3 will call default parameterized Constructor
	Triangle triangle3(2);
  	triangle3.print();


}
S.Y

Konstruktor w CPP

// C++ program to demonstrate constructors
 
#include <bits/stdc++.h>
using namespace std;
class Geeks
{
    public:
    int id;
     
    //Default Constructor
    Geeks()
    {
        cout << "Default Constructor called" << endl;
        id=-1;
    }
     
    //Parameterized Constructor
    Geeks(int x)
    {
        cout << "Parameterized Constructor called" << endl;
        id=x;
    }
};
int main() {
     
    // obj1 will call Default Constructor
    Geeks obj1;
    cout << "Geek id is: " <<obj1.id << endl;
     
    // obj1 will call Parameterized Constructor
    Geeks obj2(21);
    cout << "Geek id is: " <<obj2.id << endl;
    return 0;
}
Worried Wolverine

C Konstruktory

class MyClass {     // The class
  public:           // Access specifier
    MyClass() {     // Constructor
      cout << "Hello World!";
    }
};

int main() {
  MyClass myObj;    // Create an object of MyClass (this will call the constructor)
  return 0;
}
Magnificent Monkey Adi

C konstruktor

// C++ program to demonstrate the use of default constructor

#include <iostream>
using namespace std;

// declare a class
class  Wall {
  private:
    double length;

  public:
    // default constructor to initialize variable
    Wall() {
      length = 5.5;
      cout << "Creating a wall." << endl;
      cout << "Length = " << length << endl;
    }
};

int main() {
  Wall wall1;
  return 0;
}
Repulsive Raven

C konstruktor

class Book {public:    Book(const char*);    ~Book();    void display();private:    char* name;};
Mysterious Mamba

Odpowiedzi podobne do “Konstruktor w CPP”

Pytania podobne do “Konstruktor w CPP”

Więcej pokrewnych odpowiedzi na “Konstruktor w CPP” w C++

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu