C++: Constructor with parameters

This entry is part 26 of 61 in the series C++

Imaging you work for a record company and you need to retrieve some information about some artistes.

You can use constructor to quickly initialize the data members and print them out.

Take note that parameters are not needed for destructor.

#include <iostream>
#include <string>

using namespace std;

class Rock
{
public:

	string name;
	string track;

	Rock(string, string);  //This is the constructor
	~Rock();  //This is the destructor
};

Rock::Rock(string n2, string t2)
{
	name = n2;
	track = t2;
}

Rock::~Rock()
{
	cout << "This is Rod Stewart Signing Off";
	cout << endl;
}

int main()
{

	string n1 = "Rod Stewart";
	string t1 = "I was only Joking";

	Rock rs(n1,t1); // instance declaration

	cout << "This is " << rs.name << " Singing " << rs.track;
	cout << endl;

	return 0;
}

constructor1

C++: A simple experiment on Constructor/Destructor

This entry is part 25 of 61 in the series C++

We can work out a simple example to show how a constructor and desctructor work.

Destructors destroy the class objects created by constructors. The destructors have the same name as their class, preceded by a tilde (~).

This example does nothing but create the ts instance or object and once the program exits out of the main, the desctructor will be called releasing the ts instance back to the memory.

#include <iostream>
#include <string>

using namespace std;

class Country
{
public:
	Country();  //This is the constructor
	~Country();  //This is the destructor
};

Country::Country()
{
	cout << "This is Taylor Swift Singing.";
	cout << endl;
}

Country::~Country()
{
	cout << "This is Taylor Swift Signing Off.";
	cout << endl;
}

int main()
{
	Country ts; // instance declaration
	return 0;
}

When the ts instance declaration is called in line 27, the constructor function will be called automatically.

destructor

PHP & OOP: Constructors and destructors

This entry is part 8 of 11 in the series PHP & OOP

As an object-orientted programming language, PHP 5 allows developers to declare constructor methods for classes.

Classes which have a constructor method will call this method on each newly-created object.

So in a way, constructor is used to automate the task of creating a new object.

The destructor method will be called when there are no references to a particular object, or during the shutdown sequence.

In the code below, the destructor will be called when the running of php script is completed.

<?php

class PopStar
{
    public $person;
    public $salary;
    public $album;

    function __construct($pop, $sal, $alb )
    {
        echo "Construct function automatically called";
        echo "<br>";
        $this->person = $pop;
        $this->salary = $sal;
        $this->album = $alb;
    }

    function __destruct(){
        echo "Destruct function automatically called";
        echo "<br>";
    }

    public function getSalary()
    {
        echo $this->salary;
    }
}

$star = new PopStar("Madonna", 10000, "Crazy For You");
echo "The pop star salary is:";
echo "<br>";
$star->getSalary();
echo "<br>";

?>

The output from running the above script is:

construct