Skip to content

Instantly share code, notes, and snippets.

@nathanielanozie
Created November 4, 2023 20:03
Show Gist options
  • Save nathanielanozie/c3817f131c81c9236103990d016d6cf3 to your computer and use it in GitHub Desktop.
Save nathanielanozie/c3817f131c81c9236103990d016d6cf3 to your computer and use it in GitHub Desktop.
for python and c++ side by side
//for python and c++ side by side
//
//save this file as:cpp_doodle.cpp
//compile it in terminal by: g++ -o prog cpp_doodle.cpp
//run it in terminal by: ./prog
#include <iostream> //for printing
#include <vector> //for std vector
#include <map> //for map, pair
void dFun()
{
std::cout<<"dFun huraay!"<<std::endl;
}
class E
{
public:
E(int num=21): _num(num){ printf("E huraay!\n"); }
private:
int _num;
};
class G
{
public:
G(int num=21): _num(num){}
void gFun(){ printf("gFun from a class %d\n", _num); }
private:
int _num;
};
class H
{
public:
H();
static void hFun(){printf("hFun from a class\n");}
};
struct L
{
std::string _a;
int _b;
std::vector<int> _c;
L(std::string a1, int b1, std::vector<int> c1): _a(a1), _b(b1), _c(c1){}
};
class M
{
public:
M(){printf("M constructor called\n");} //base class constructor automatically called in sub classes since constructor has no arguments
};
class N: public M
{
public:
N(){
printf("N constructor called\n");
}
};
class M1
{
public:
M1();
M1(int num){printf("M1 constructor called\n");}
};
class N1: public M1
{
public:
N1(int num=21) : M1(num){ //need to call base class constructor because it has parameters
printf("N1 constructor called\n");
}
};
void u(int arg)
{
std::cout<<arg<<std::endl;
}
void u(double arg)
{
std::cout<<arg<<std::endl;
}
void u(std::string arg)
{
std::cout<<arg<<std::endl;
}
class VAbstract
{
public:
virtual void printMe(){printf("printMe from VAbstract\n");}
};
class V: public VAbstract
{
public:
V(){};
void printMe(){printf("printMe from V\n");}
};
class V1: public VAbstract
{
public:
V1(){};
void printMe(){printf("printMe from V1\n");}
};
void vPrint(VAbstract *vArg)
{
vArg->printMe();
}
int w()
{
return 21;
}
bool x()
{
return true;
}
int main()
{
int a = 21;
//b
std::vector<int> b;
b.push_back(19);
b.push_back(20);
b.push_back(21);
std::vector<int>::iterator intIter;
for(intIter=b.begin(); intIter != b.end(); intIter++)
{
std::cout<<*intIter<<std::endl;
}
//c
std::vector< std::pair<std::string, int> > c;
std::pair<std::string, int> c1;
c1 = std::make_pair("a",19);
c.push_back(c1);
c1 = std::make_pair("b",20);
c.push_back(c1);
c1 = std::make_pair("c",21);
c.push_back(c1);
std::vector< std::pair<std::string, int> >::iterator cIter;
for(cIter = c.begin(); cIter != c.end(); cIter++)
{
std::cout<< cIter->first <<" "<< cIter->second<< std::endl;
}
dFun();
E *e; //making a pointer to a class
e = new E(); //new returns a pointer
delete e; //free memory
E e1; //not using pointer
E *f;
f = new E(27);
delete f; //free memory
E f1(27); //not using pointer
//using pointer
G *g;
g = new G(27);
g->gFun(); //calling a method from a pointer
delete g;
//not using pointer
G g1(27);
g1.gFun();
H::hFun();
std::map<std::string, int> i;
i["a"]=19;
i["b"]=20;
i["c"]=21;
std::map<std::string, int>::iterator iIter;
for(iIter=i.begin(); iIter!=i.end(); iIter++)
{
std::cout<<iIter->first<<" "<<iIter->second<<std::endl;
}
bool j = true;
std::string k = "huraay";
std::vector<int> lvec;
lvec.push_back(3);
lvec.push_back(4);
L l("_a", 2, lvec);
std::cout<< l._a <<std::endl;
std::cout<< l._b <<std::endl;
std::vector<int> lv = l._c;
for(std::vector<int>::iterator lIter = lv.begin(); lIter != lv.end(); lIter++)
{
std::cout<< *lIter << std::endl;
}
N n;
N1 n1(21);
for(int o=0; o<10; o++)
{
printf("%d\n",o);
}
bool p = true;
if(p)
{
printf("phantastic!\n");
}
else{
printf("thats cool\n");
}
std::string q = "c++ extras: enum, typdef, pointer";
std::cout<< q <<std::endl;
char r = 'a';
std::cout<< r <<std::endl;
std::string s = "c++ extras: , namespace, template functions, template classes, virtual class methods for inheritance";
std::cout<< s <<std::endl;
double t = 21.0;
std::cout<< t <<std::endl;
u(21);
u(21.0);
u("Twenty One");
V *v;
v = new V();
vPrint(v);//vPrint method accepts VAbstract pointer but can show children classes implementation
delete v; //freeing memory
V1 *v1;
v1 = new V1();
vPrint(v1); //same method works with a different class
delete v1;
int w1=w();
printf("%d\n",w1);
bool x1=x();
std::cout<< x1 <<std::endl;
/*
multi line comment
y
*/
std::string z = "Thanks for Reading!";
std::cout<< z <<std::endl;
return 0;
}
#for python and c++ side by side
#
#save this file as:py_doodle.py
#run it in terminal by:python py_doodle.py
a = 21 #int
b = [19, 20, 21] #std::vector
print(b)
c = [("a",19), ("b",20), ("c",21)] #std::vector of std::pair's
print(c)
def dFun():
print("dFun huraay!")
dFun()
class E():
def __init__(self, num=21): #constructor with default parameter
print("E huraay!")
self._num = num #initializing a class variable
e = E() #creating an instance of class
f = E(27)
class G(object): #inheritance
def __init__(self, num=21):
self._num = num
def gFun(self): #method in a class
print("gFun from a class", self._num) #using a class variable
g = G(27)
g.gFun() #calling a class method
class H(object):
def __init__(self):
pass
@classmethod
def hFun(self): #like static method
print("hFun from a class")
H.hFun() #calling a static method
i = {"a":19, "b":20, "c":21} #like std::map
print(i)
j = True #bool
k = "huraay" #std::string
class L(object): #like a simple struct
def __init__(self):
self._a = "_a"
self._b = 2
self._c = [3,4]
l = L()
print(l._a, l._b, l._c) #acessing elements of struct
class M(object):
def __init__(self):
print("M constructor called")
class N(M): #inheritance
def __init__(self):
super(N, self).__init__() #calling parent constructor
print("N constructor called")
n = N()
for o in range(0,10): #for loop 0 to 9
print(o)
p = True
if p: #if statement
print("phantastic!")
else:
print("thats cool")
q = "c++ extras: enum, typdef, pointer"
print(q)
r = 'a' #char
print(r) #formatted print char
s = "c++ extras: , namespace, template functions, template classes, virtual class methods for inheritance"
print(s)
t = 21.0 #double (more accurate) or float
print(t) #formatted print double
def u1(arg=21):
print(arg)
def u2(arg=21.0):
print(arg)
def u3(arg="Twenty One"):
print(arg)
u1(21)
u2(21.0)
u3("Twenty One")
print("like c++ function overloading using different types")
class VAbstract(object):
def __init__(self):
pass
def printMe(self):
print("printMe from VAbstract")
class V(VAbstract):
def __init__(self):
pass
def printMe(self):
print("printMe from V")
v = V() #start for learning about virtual class methods using derived classes implementation
v.printMe()
def w(): #method returning an int
return 21
w1 = w()
print(w1)
def x(): #method returning a bool
return True
x1 = x()
print(x1)
"""
multi line comment
y
"""
z = "Thanks for Reading!"
print(z)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment