Constructor en C++ es un método especial que se invoca automáticamente en el momento de la creación del objeto. Se utiliza para inicializar los miembros de datos de nuevos objetos en general. El constructor en C++ tiene el mismo nombre que la clase o estructura. El constructor se invoca en el momento de la creación del objeto. Construye los valores, es decir, proporciona datos para el objeto, por lo que se conoce como constructores.
Constructor no tiene un valor de retorno, por lo tanto, no tienen un tipo de retorno.
C++
// defining the constructor within the class #include <iostream> using namespace std; class student { int rno; char name[10]; double fee; public: student() { cout << "Enter the RollNo:"; cin >> rno; cout << "Enter the Name:"; cin >> name; cout << "Enter the Fee:"; cin >> fee; } void display() { cout << endl << rno << "\t" << name << "\t" << fee; } }; int main() { student s; // constructor gets called automatically when // we create the object of the class s.display(); return 0; }
C++
// defining the constructor outside the class #include <iostream> using namespace std; class student { int rno; char name[50]; double fee; public: student(); void display(); }; student::student() { cout << "Enter the RollNo:"; cin >> rno; cout << "Enter the Name:"; cin >> name; cout << "Enter the Fee:"; cin >> fee; } void student::display() { cout << endl << rno << "\t" << name << "\t" << fee; } int main() { student s; s.display(); return 0; }
C++
#include <iostream> using namespace std; class Line { public: void setLength( double len ); double getLength( void ); Line( double len ); //This is the constructor private: double length; }; //Member function definition including constructor Line::Line( double len ) { cout<<"Object is being created , length ="<< len <<endl; length = len; } void Line::setLength( double len ) { length = len; } double Line::getLength( void ) { return length; } //Main function for the program int main() { Line line(10.0); //get initially set length cout<<"Length of line :" << line.getLength() << endl; //set line length again line.setLength(6.0); cout<<"Length of line :" << line.getLength() << endl; return 0; }
CPP
// Cpp program to illustrate the // concept of Constructors #include <iostream> using namespace std; class construct { public: int a, b; // Default Constructor construct() { a = 10; b = 20; } }; int main() { // Default constructor called automatically // when the object is created construct c; cout << "a: " << c.a << endl << "b: " << c.b; return 1; }
C++
// Example #include<iostream> using namespace std; class student { int rno; char name[50]; double fee; public: student() // Explicit Default constructor { cout<<"Enter the RollNo:"; cin>>rno; cout<<"Enter the Name:"; cin>>name; cout<<"Enter the Fee:"; cin>>fee; } void display() { cout<<endl<<rno<<"\t"<<name<<"\t"<<fee; } }; int main() { student s; s.display(); return 0; }
CPP
// CPP program to illustrate // parameterized constructors #include <iostream> using namespace std; class Point { private: int x, y; public: // Parameterized Constructor Point(int x1, int y1) { x = x1; y = y1; } int getX() { return x; } int getY() { return y; } }; int main() { // Constructor called Point p1(10, 15); // Access values assigned by constructor cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY(); return 0; }
C++
// Example #include<iostream> #include<string.h> using namespace std; class student { int rno; char name[50]; double fee; public: student(int,char[],double); void display(); }; student::student(int no,char n[],double f) { rno=no; strcpy(name,n); fee=f; } void student::display() { cout<<endl<<rno<<"\t"<<name<<"\t"<<fee; } int main() { student s(1001,"Ram",10000); s.display(); return 0; }
CPP
// Illustration #include <iostream> using namespace std; class point { private: double x, y; public: // Non-default Constructor & // default Constructor point(double px, double py) { x = px, y = py; } }; int main(void) { // Define an array of size // 10 & of type point // This line will cause error point a[10]; // Remove above line and program // will compile without error point b = point(5, 6); }
C++
// Implicit copy constructor #include<iostream> using namespace std; class Sample { int id; public: void init(int x) { id=x; } void display() { cout<<endl<<"ID="<<id; } }; int main() { Sample obj1; obj1.init(10); obj1.display(); Sample obj2(obj1); //or obj2=obj1; obj2.display(); return 0; }
C++
// Example: Explicit copy constructor #include <iostream> using namespace std; class Sample { int id; public: void init(int x) { id=x; } Sample(){} //default constructor with empty body Sample(Sample &t) //copy constructor { id=t.id; } void display() { cout<<endl<<"ID="<<id; } }; int main() { Sample obj1; obj1.init(10); obj1.display(); Sample obj2(obj1); //or obj2=obj1; copy constructor called obj2.display(); return 0; }
C++
#include<iostream> #include<string.h> using namespace std; class student { int rno; char name[50]; double fee; public: student(int,char[],double); student(student &t) //copy constructor { rno=t.rno; strcpy(name,t.name); fee=t.fee; } void display(); }; student::student(int no,char n[],double f) { rno=no; strcpy(name,n); fee=f; } void student::display() { cout<<endl<<rno<<"\t"<<name<<"\t"<<fee; } int main() { student s(1001,"Manjeet",10000); s.display(); student manjeet(s); //copy constructor called manjeet.display(); return 0; }
C++
#include<iostream> #include<string.h> using namespace std; class student { int rno; char name[50]; double fee; public: student(int,char[],double); student(student &t) //copy constructor (member wise initialization) { rno=t.rno; strcpy(name,t.name); } void display(); void disp() { cout<<endl<<rno<<"\t"<<name; } }; student::student(int no, char n[],double f) { rno=no; strcpy(name,n); fee=f; } void student::display() { cout<<endl<<rno<<"\t"<<name<<"\t"<<fee; } int main() { student s(1001,"Manjeet",10000); s.display(); student manjeet(s); //copy constructor called manjeet.disp(); return 0; }
C++
#include <iostream> using namespace std; class Test { public: Test() { cout << "\n Constructor executed"; } ~Test() { cout << "\n Destructor executed"; } }; main() { Test t; return 0; }
C++
#include <iostream> using namespace std; class Test { public: Test() { cout << "\n Constructor executed"; } ~Test() { cout << "\n Destructor executed"; } }; main() { Test t, t1, t2, t3; return 0; }
C++
#include <iostream> using namespace std; int count = 0; class Test { public: Test() { count++; cout << "\n No. of Object created:\t" << count; } ~Test() { cout << "\n No. of Object destroyed:\t" << count; --count; } }; main() { Test t, t1, t2, t3; return 0; }
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA