C++: operator overloading -> output operator <<

The output operator << works only with the primitive types. It is required to overload the operator in order to have a custom output of complex types like structs and classes.

Here’s is an example below :

We create a class named Customer

class Customer {
public:
	string m_name;
	string m_email;
	int m_phone;

	Customer(string name, string email, int phone)
	{
		m_name = name;
		m_email = email;
		m_phone = phone;
	}
	
};

Now from our main function if we initialize and create 3 customers:

int main()
{
	
	Customer c1("John Doe", "john@test.com", 22536598);
	Customer c2("Mary Jane", "mary@test.com", 22536545);
	Customer c2("Janet May", "janet@test.com", 22536598);

	cout << c1;
	cin.get();
	return 0;
}

the line cout << c1 will not compile. It will throw the error

E0349 no operator “<<” matches these operands

cout is an object of the ostream class and its implementation of insertion operator (<<) does not cover user defined types. It is overloaded only for arithmetic types, stream buffers and manipulators.

It is clear from the above fact that if we overload the insertion operator (<<) in our complex type we can get cout to output our data the way we want.

I will overload the insertion operator for our Customer class. Our code will look like this

class Customer {
public:
	string m_name;
	string m_email;
	int m_phone;

	Customer(string name, string email, int phone)
	{
		m_name = name;
		m_email = email;
		m_phone = phone;
	}
	friend ostream& operator<<(std::ostream& os, const Customer &p);
};


ostream & operator <<(std::ostream &os, const Customer& c) {
	os << "Name: " << c.m_name << endl << "Phone:" << c.m_phone << endl << "Email:" << c.m_email << endl;
	return os;
}

Note that insertion operator is overloaded globally. This is because operator requires lhs parameter to be of the type of ostream, which is possible only if the overloaded operator is a member of ostream.

The output from our main function will be as below:

 Name: John Doe
 Phone:22536598
 Email:john@test.com 

Create a custom theme in Opencart

I had a requirement to change the theme of opencart website. Initial thought was to search for available out of the box themes in the market and buy one of them. My requirement was to make few changes to the layout and unfortunately none of the designs or templates met my requirement.

That’s when I decided to create my own theme. If you want to do the same follow the below steps:

Please note: all the paths mentioned below are for mac os x. Please modify them as per your operating system.

  1. Locate the default theme folder. The default theme folder is located at <webroot> /catalog/views/theme/default
    1. Make a copy of this folder and rename it to the name of your theme. It is advisable to keep the name of your theme without any special characters and with out space.
    1. Navigate to template/common folder inside this new theme folder and edit the header.twig file.
    1. Find <link href=”catalog/view/theme/default/stylesheet/stylesheet.css” rel=”stylesheet”>
    1. The href points to stylesheet in your default theme folder. Change it with the name of <yourtheme> to make it point to your theme folder
  2. Create language file for theme. Navigate to admin folder. <webroot>/admin/language/en-gb/extension/theme/
    1. Copy the file default.php and rename it to <yourtheme>.php
    1. Open the <yourtheme>.php file and find
      $_[‘heading_title’]                    = ‘Default Store Theme’;
    1. Change the value to title of your own theme.
  3. Create a new twig file. Navigate to <webroot>/admin/view/template/extension/theme
    1. Copy default.twig and rename it to <yourtheme>.twig
    1. Find and replace all “default” with <yourtheme>
  4. Create a controller for the theme
    1. Navigate to <webroot>/admin/controller/extension/theme
    1. Copy default.php and rename it to <yourtheme>.php
    1. Find and replace all “default” with <yourtheme>
    1. Make sure to remove any underscore in the name of your theme if it has any while changing the name of controller.
  5. Go to opencart admin panel and open Extensions tab
  6. Choose Theme from the drop down. Your theme will be listed.
  7. Go ahead and install your theme. After installation, your theme will show “disabled” status against your current store. To enable it, click on the edit icon and select “enable” from the drop down.
  8. Navigate to System>Settings>General. From the Theme dropdown you can now select your theme.

You can now go ahead and modify this theme to suit your needs and save lot of money.

Cheers !