Wednesday, March 15, 2023

doodle

Hi, 

c++ doodle creating a simple vertex struct (kindof like a container of data and possibly methods) and using a template function (kindof like a way to have same method work with different types of parameters hence reducing code duplication).  i think c++ is different in python in that i think c++ cares alot about the types of variables and what types are in method parameters and being returned whereas i think python takes care of picking types for variables when you assign a variable something.  like in python can create a variable  var = [] or var = '' or var = {} and python takes care of the types but in c++ similar to mel need to explicitly tell it the type like: int var or double var etc. i think there are lots of other ways c++ is different from python.

#include <iostream>

//for writing to file
#include <fstream>
#include <iostream>
//for reading file
#include <sstream>


struct vertStruct
{
	double x;
	double y;
	double z;
	
	vertStruct( double x1, double y1, double z1) //this is the constructor - how we can create this struct
	{
		x = x1;
		y = y1;
		z = z1;
	}
	
};

//another way of writing the same struct above
struct vertStructB
{
	double x;
	double y;
	double z;
	
	vertStructB( double x1, double y1, double z1) : x(x1), y(y1), z(z1) {} //using a shorthand form for initializing the variables of struct
};

//example of a method that doesnt return anything
void print_vertStruct( vertStruct &v ) //using & so passing parameter as reference so not copying the struct
{
	std::cout<<"printing vertStruct"<<std::endl;
	std::cout<<"x: "<<v.x<<" "<<"y: "<<v.y<<" "<<"z: "<<v.z<<std::endl;
}
void print_vertStructB( vertStructB &v )
{
	std::cout<<"printing vertStructB"<<std::endl;
	std::cout<<"x: "<<v.x<<" "<<"y: "<<v.y<<" "<<"z: "<<v.z<<std::endl;
}

//using a template function so could print both structs with same method
template<class V>
void print_vert(V v)
{
	std::cout<<"printing a vert"<<std::endl;
	std::cout<<"x: "<<v.x<<" "<<"y: "<<v.y<<" "<<"z: "<<v.z<<std::endl;	
}


int main()
{
	std::cout<<"topics hurraay"<<std::endl;
	
	/*
	//writing to a text file
	std::string vstr = "great wrote some text info\n";
	
	std::ofstream ofile("verts.txt");
	ofile << vstr;
	ofile.close();
	//https://stackoverflow.com/questions/15388041/how-to-write-stdstring-to-file
	*/
	
	//reading from a text file
	//ex 1
	/*
	std::string ivstr;
	std::ifstream ifile("verts.txt");
	std::getline(ifile, ivstr);
	std::cout<<ivstr<<std::endl;
	*/
	
	/*
	//ex 2
	std::string ivstr;
	std::ifstream ifile("verts_dat.txt");
	//format of text file:
	//x,y,z
	//10 20 30
	//5 10 15
	//25 32 10
	
	//skip first line
	std::getline(ifile, ivstr);
	
	double x,y,z;
	//go through each line of text file
	while(std::getline(ifile, ivstr))
	{
		std::istringstream iss(ivstr);
		//save into variables
		iss >> x >> y >> z;
		
		std::cout<<"x,y,z:"<<x<<" "<<y<<" "<<z<<std::endl;
	}
	
	//x,y,z:10 20 30
	//x,y,z:5 10 15
	//x,y,z:25 32 100
	//https://stackoverflow.com/questions/51007121/read-a-text-file-c
	*/
	
	
	//example with struct and example with template function
	vertStruct v(10,20,30);
	//std::cout<<"x: "<<v.x<<" "<<"y: "<<v.y<<" "<<"z: "<<v.z<<std::endl;
	
	//using method to print
	print_vertStruct(v);
	
	vertStructB v1(40,50,60);
	print_vertStructB(v1); //would error if used v as parameter because its not the parameter type in the method
	
	//should work for both types of structs
	print_vert(v); 
	print_vert(v1);
	
	//output from example
	//topics hurraay
	//printing vertStruct
	//x: 10 y: 20 z: 30
	//printing vertStructB
	//x: 40 y: 50 z: 60
	//printing a vert
	//x: 10 y: 20 z: 30
	//printing a vert
	//x: 40 y: 50 z: 60
	
	return 0;
}

//g++ -o prog topicsA.cpp
//./prog
Happy Sketching!