Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
1.
#ifndef T_HPP_INCLUDED
#define T_HPP_INCLUDED
#include <iostream>
using namespace std;
#include <math.h>
class T{
public:
        static int t;
        T(){++t;}
};

class S:public T{
public:
        static int s;
        S(){++s;}
        ~S(){s++;}
        S test(){
        S ss;
        return ss;
        }
};

int T::t=pow(2,2);
int S::s=sqrt(16);
int main()
{
    T t1;
    S s1,s2,s3;
    s2.test();
    cout<<S::s<<T::t;
    return 0;
}
2.
#ifndef A_HPP_INCLUDED
#define A_HPP_INCLUDED
#include <iostream>
using namespace std;
template<int vred,class tip<
class KlasaA{
private:
    tip i;
    tip j;
public:
    KlasaA(){i=vred; j=vred;}
};
int main()
{
    KlasaA<-2,double>gen1(0,1);
    KlasaA<3,int>gen2;
    return 0;
}
3.
#ifndef B_HPP_INCLUDED
#define B_HPP_INCLUDED
#include <iostream>
using namespace std;

class B:public A{
public:
    void p(){cout<<"A";}
};
void f(A& ra){
    ra.p();
}
int main()
{
   B b;
   b.p();
   f(b);
    return 0;
}

#endif // B_HPP_INCLUDED
4.
#ifndef A_HPP_INCLUDED
#define A_HPP_INCLUDED
#include <iostream>
using namespace std;
class A{
public:
    A(){cout<<"1";}
    virtual void show(){cout<<"2";}
};

class Q:public A
{
public:
    Q(){cout<<"3";}
    void show(){cout<<"4";}
    ~Q(){cout<<"5";}
};

int main()
{
    A a;
    Q q;
    A *pokA;
    a.show();
    pokA=&q;
    pokA->show();
    return 0;
}
#endif // A_HPP_INCLUDED
5.
class A
{
private:
	int a,b;
public:
	A(){a=1; b=2;}
	A(int aa=1,int bb=2){a=aa; b=bb;}
	A(const A& aa){a=aa.a; b=aa.b;}
	void ispis(){
		cout<<++a<<b++;
	}
};

int main()
{
	A a,a2(3,4),a3(a2);
	a.ispisi();
	a2.ispisi();
	a3.ispisi();
}
6. This is c#
public static void Main(string[]args)
{
	A a1,a2,a3,a4;
	a1=new A(1);
	a2=new A(2);
	a3=new A(3);
	a4=new A(4);
	a2=a1;
	a3=a2;
	List<a>list=new List</a><a>();
	list.Add(a1);
	list.Add(a2);
	list.Add(a3);
	list.Add(a4);
	a1=a4;
	foreach(A a in list){
		Console.Write(a.a);
        }
}


What I have tried:

I know i can put the code in Code Blocks and build it,but i dont understand the results.
Posted
Updated 25-Jan-19 5:32am
v2
Comments
Dave Kreskowiak 24-Jan-19 17:29pm    
Nope. This screams "homework assignment" and we're not in the business of doing your work for you.
IvaSerbia 24-Jan-19 17:35pm    
Doing my homework? 😂Brother, like i said, i can put the code in CodeBlocks and get the results. The problem is, i don't understand why is some numbers results of my code. If you angry,leave out my post.
Mohibur Rashid 24-Jan-19 21:22pm    
You are not in position to tell who is going to answer/comment on your post and who can't.
Stefan_Lang 25-Jan-19 10:13am    
Then tell us what numbers you get and why you think it should be different. That will tell us a lot about what, exactly you don't understand. Without at least that much information, our explanations might as well start with the basics of Turing Machines.
David O'Neil 24-Jan-19 17:52pm    
What result don't you understand?

Quote:
Can someone explain me thoose codes in detail step-by-step.

Why not ask your computer to do it ?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
IvaSerbia 25-Jan-19 5:53am    
I want to understand how program gave that results. Can i do that with breakpoints?
Sorry for my bad English..
Patrice T 25-Jan-19 8:24am    
Execute your program step by step, the debugger will show you changes done every time.
Stefan_Lang 25-Jan-19 10:21am    
This is probably the best advice to offer, given such an unspecified question. 5.
Patrice T 25-Jan-19 11:57am    
Thank you.
BillWoodruff 25-Jan-19 23:43pm    
+5 Spot on !
Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?
 
Share this answer
 
Not trying to actually compile, but just looking at this code, this is what I can say about it at a glance:

Code 1 is all about knowing when a constructor or destructor is called. The static variables T::t and S::s simply count these calls (with a little variation). If you want to understand how and when these values are changed, simply put breakpoints at every line where T::t and S::s are changed, and inspect the call stack whenever the debugger hits one of these points. Hint: you should insert some linebreaks so that each instruction is on a separate line! That helps for setting breakpoints, and knowing where the program counter actually is during debugging.

Code 2 does not compile, and does not produce any output, even when fixed. What is the question?

Code 3 also doesn't compile: at the very least it misses the entire declaration for class A. However, unless the constructor of A prints something too, it would probably print out "AA" - but I have no idea at all what aspect of C++ programming is supposed to be tested here.

Code 4 appears to be a simple showcase for virtual inheritance, showing when and which function/constructor/destructor gets called - it really should be a little more wordy than "3", "4", "5" though ...

Code 5 shouldn't compile unless the compiler is very forgiving, and it's options set to accept 'lousy code'. If so, it may output something like "224444"; I say 'may' because I'm not sure about the order of evaluation in the line that declares a, a2, and a3 and I'm too lazy to look it up ;-) .

Code 6 is C# and therefore outside of my field of expertise, but it seems to be missing the declaration of A.
 
Share this answer
 
You need to learn the languages to understand it completly. But this are some simple classeswhich do some things. The most complicated is that some class template is used. It is a bit like text which gets expanded to some hugh source code.

I think that some bugs are in the code and you shouldnt rely on the quality of it. Dont tell that you wrote it ;-)
 
Share this answer
 
Comments
IvaSerbia 25-Jan-19 5:56am    
Those codes I had on my exam questions, and they asked us what will be written. We can't use any program, we just have this code and question - What will be written at the end. That's the reason why I'm asking someone to explain me step by step or give me some advice how to understand that.
Sorry for my bad English.
KarstenK 25-Jan-19 8:06am    
Download Visual Studio and include them in some projects. You must separate C++ and C# for simplicity. Try it out and learn...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900