conversion using constructor &&&& cast () operator overloading

---------Project for converting by constructor method---------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//main.cpp
#include <iostream>

using namespace std;

#include "truck.h"
#include "car.h"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	
	
	cout << "---------Program Initiated-----------" << endl << endl;
	
	cout << endl << "Conversions by constructors" << endl << endl;
	
	car cobj;	
	truck * ptrtruck = new truck(cobj);
	
	truck tobj;	
	car * ptrcar = new car(tobj);
	
	cout << "simple car\t";
	cobj.print();
	cout << "car converted\t";
	ptrcar->print ();    // Added print
	
	cout << "simple truck\t";
	tobj.print();
	cout << "truck converted\t";
	ptrtruck->print ();    // Added print
	
	cout << endl << endl << endl << endl << "Conversions by cast () operator" << endl << endl;
	
	cout << "Didn't write the cast overloading code but try direct conversion using cast operator" << endl;
	cout << "Convert to car" << endl;
	car newcar = (car)tobj;
	newcar.print();
	
	cout << endl << "Now convert to truck" << endl;
	truck newtruck = (truck)cobj;
	newtruck.print();

	delete ptrtruck;
	delete ptrcar;
	
	system("pause");
	
	return 0;
	
}//---------main() 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//car.h
#ifndef CAR_H
#define CAR_H

class truck;		// forward added

class car
{
	public:
		
		std::string type;
		int year;
		
		car();	
		car(truck &);		
		void print();
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//car.cpp
#include <string>		//	Added include
#include <iostream>		//	Added include
#include "car.h"		//	Added include
#include "truck.h"		//	Added include 

car::car()
{	type = "Car";
	year = 2014;	
}//------constructor

car::car(truck & tobj)
{	type = "c-Car";
	year = tobj.year;
	
}//------------constructor(truck)

void car::print()
{	std::cout << "Type: " << type << "\tYear: " << year << std::endl;
}//---------print() 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//truck.h
#ifndef TRUCK_H
#define TRUCK_H

class car;

class truck
{
	public:
		
		std::string type;
		int year;
		
		truck();	
		truck(car &);		
		void print();
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//truck.cpp
#include <string>		//	Added include
#include <iostream>		//	Added include
#include "car.h"		//	Added include
#include "truck.h"		//	Added include

truck::truck()
{	type = "Truck";
	year = 2012;	
}//------constructor()

truck::truck(car & cobj)
{	type = "c-Truck";
	year = cobj.year;		
}//--------constructor(car)

void truck::print()
{	std::cout << "Type: " << type << "\tYear: " << year << std::endl;	
}//----------print() 


----------Project for converting using operator overloading method-------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//main.cpp
#include <iostream>

using namespace std;

#include "truck.h"
#include "car.h"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	
	
	cout << "---------Program Initiated-----------" << endl << endl;
	
	cout << "conversion using cast () operator" << endl << endl;
	
	car cobj;
	
	truck tobj;
	
	car ncobj = (car)tobj;
	
	truck ntobj = (truck)cobj;
	
	cout << "simple car\t";
	cobj.print();
	
	cout << "simple truck\t";
	tobj.print();
	
	cout << endl << "converted from car to truck" << endl;
	ntobj.print();
	
	cout << endl << "converted form truck to car" << endl;
	ncobj.print();
	
	cout << endl << endl << endl << endl << "Didn't write conversion constructor but let's try" << endl << endl;
	
	cout << "conversion using constructor" << endl << endl;
	
	cout << "convert from truck to car" << endl;
	car newcar = car(tobj);
	newcar.print();
	
	cout << endl << "now convert from car to truck" << endl;
	truck newtruck = truck(cobj);
	newtruck.print();
	
	cout << endl << endl;
	
	system("pause");
	
	return 0;
	
}//---------main() 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//car.h
#ifndef CAR_H
#define CAR_H

class truck;		// forward added

class car
{
	public:
		
		std::string type;
		int year;
		
		car();
		operator truck() const;
		void print();
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//car.cpp
#include <string>		//	Added include
#include <iostream>		//	Added include
#include "car.h"		//	Added include
#include "truck.h"		//	Added include 

car::car()
{	type = "Car";
	year = 2014;	
}//------constructor

car::operator truck() const{
	
	truck temp;
	temp.type = "c-truck";
	temp.year = this->year;
	return temp;
	
}//------operator truck()

void car::print()
{	std::cout << "Type: " << type << "\tYear: " << year << std::endl;
}//---------print() 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//truck.h
#ifndef TRUCK_H
#define TRUCK_H

class car;

class truck
{
	public:
		
		std::string type;
		int year;
		
		truck();
		operator car() const;
		void print();
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//truck.cpp
#include <string>		//	Added include
#include <iostream>		//	Added include
#include "car.h"		//	Added include
#include "truck.h"		//	Added include

truck::truck()
{	type = "Truck";
	year = 2012;	
}//------constructor()

truck::operator car() const{
	
	car temp;
	temp.type = "c-car";
	temp.year = this->year;
	return temp;
	
}//---------operator car()

void truck::print()
{	std::cout << "Type: " << type << "\tYear: " << year << std::endl;	
}//----------print() 


I'm a student of BSSE Semester 1st and obviously in initial learning this might seem too trivial code to you guys but for me It's a latter useable tutorial for myself at-least

Thanks, I appreciate any(appreciating/criticizing) type of comments, and obviously thanks again for viewing my code.
Consider the C++ cast syntax instead of C-style cast.
ummm.... and what is C++ cast syntax

the one I used is just

class::operator type() const {//code ending with a return statement};

is this C style???

and what is C++ style???

also pardon me I didn't even think about overloading in C, I thought that overloading was a c++ concept but thanks for adding to my knowledge
Topic archived. No new replies allowed.