Jump to content

good place to learn java/c++ classes.

Cookiecrumbles222

hay guys i'm in java/c++ classes and the current topic is classes, i'm not sure if that the right world, but its the thing where you make two separate class projects and to make public and private functions and then call on them in the other page (example below). but i have no idea how to do it and i'm falling behind, so if anyone knows a good why to explain this stuff or where i can find how to do it, it would be much appreciated. thank you!


public class functions {

 

public class Fraction

{

  private final int numerator;

  private final int denominator;

 

  public Fraction(int num, int denom)

  {

     

  }

  

  public Fraction add(Fraction other)

  {

     

  }

 

  public Fraction subtract(Fraction other)

  {

     

  }

 

  public String toString()

  {

     return numerator + "/" + denominator;

  }

}


 

}

 

Link to comment
Share on other sites

Link to post
Share on other sites

There are no functions in Java, in object oriented programming it's called methods.

 

You should read a book... The Java Programming Language by J. Gosling et. al.

 

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, Audio Carnage said:

There are no functions in Java, in object oriented programming it's called methods.

 

Quote

In computer programming, a subroutine is a sequence of program instructions that perform a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed. Subprograms may be defined within programs, or separately in libraries that can be used by multiple programs. In different programming languages, a subroutine may be called a procedure, a function, a routine, a method, or a subprogram. The generic term callable unit is sometimes used.[1]

Let's not get too pedantic here. :3

Link to comment
Share on other sites

Link to post
Share on other sites

44 minutes ago, M.Yurizaki said:

Let's not get too pedantic here. :3

I was going to dismantle them myself but alas you beat me to it as I've been at work. I love it though xD

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

12 hours ago, M.Yurizaki said:

 

Let's not get too pedantic here. :3

pedantic? it's called being accurate :dry:.

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, Audio Carnage said:

pedantic? it's called being accurate :dry:.

Do you say Kibi, Mebi, Gibi, and Tebi?

 

Anyway, considering that a method and function are subroutines, they're pretty much synonymous when used in casual lexicon. If you're being formal and there's a formal definition, then sure.

 

But we're not being formal here.

Link to comment
Share on other sites

Link to post
Share on other sites

I may have made some mistakes, no one is perfect. So take it easy when correcting me.

 

Abstraction

What you are talking about is called abstraction.

Wikipedia (Abstraction):

Quote

A technique for arranging complexity of computer systems. It works by establishing a level of complexity on which a person interacts with the system, suppressing the more complex details below the current level. The programmer works with an idealized interface (usually well defined) and can add additional levels of functionality that would otherwise be too complex to handle.

When speaking of abstraction, the "higher" the abstraction layer is the farther away from the raw code you are. Generally speaking high levels of abstraction are a good thing especially when working in a team since it makes global understanding a given. Only the person who made a piece of code knows exactly how it works and others may not have time to read the hundreds of lines and make sense of them. To this end a method or class can be made with documentation and other programmers can interact with the software as they see fit.

Classes and methods serve to make a program easier to understand and minimize duplicate code. They can be used for additional purposes but when you are getting started that's all you really need to worry about.

 

 

A simple example is:

I recently wrote a version control program and thought I'd add RGB functionality to it. Instead of going over every window and component in the program, I editted the class responsible for designing elements and made it call an RGB generator. The entire RGB logic is stored in its own class, it is built of a bunch of methods. The class can be taken and placed in other programs since it is self sufficient.

 

public class HSLColorHandler
{
	private float Q;
	private float curQ = 0;

	private Color newC()
	{
		float hslF[] = new float[3];
		hslF[0] = (curQ/Q)*0.7f;
		hslF[1] = 1f;
		hslF[2] = 0.6f;
		curQ += 1;
		int l = makeRGB(hslF);
		return new Color(l);
	}

	private int makeRGB(float[] hsl)
	{
		float alpha = 1.0f;
		float h = hsl[0];
		float s = hsl[1];
		float l = hsl[2];

		float q = 0;

		if (l < 0.5)
			q = l * (1 + s);
		else
			q = (l + s) - (s * l);

		float p = 2 * l - q;

		int r = (int)(255*Math.max(0, HueToRGB(p, q, h + (1.0f / 3.0f))));
		int g = (int)(255*Math.max(0, HueToRGB(p, q, h)));
		int b = (int)(255*Math.max(0, HueToRGB(p, q, h - (1.0f / 3.0f))));

		int alphaInt = (int)(255*alpha);

		return (alphaInt << 24) + ( r << 16 ) + ( g << 8 ) + (b);
	}

	private float HueToRGB(float p, float q, float h)
	{
		if (h < 0) h += 1;
		if (h > 1 ) h -= 1;
		if (6 * h < 1){return p+((q-p)*6*h);}
		if (2 * h < 1 ){return  q;}
		if (3 * h < 2){return p+((q-p)*6*((2.0f/3.0f)-h));}
		return p;
	}

	public Color getColor()
	{return newC();}
	public void setCurQ(int curQ){this.curQ = curQ;}
	public void setQ(int Q){this.Q = Q;}
}

 

But then how do I access it and what exactly are the benefits?

To access it you need to first create an instance of the class. You do this by writing "classTitle instanceName = new classTitle(parameters);" in this case it would be:

HSLColorHandler hsl = new HSLColorHandler();

This is because there is no constructor. We will see what a constructor is in a minute.

Once you have the instance (hsl) you can then access the class methods and variables in this manner: instanceName.methodName(parameters); in our case, if we want to get a new RGB color we would write:

Color rgb = hsl.newC();

This brings up to returns. You notice I am saying the color rgb is equal to this method "newC()" which I am running. newC() is a method with a return. This means it will run and when it is done, will return a value. In this case it is the rgb color.

You can do this with any variable EVEN with class instances.

 

What is a constructor?

In this class the constructor would have been:

public HSLColorHandler(parameters){do stuff when an instance of this class is created.}

The constructor runs immediately when the class is instantiated. It can request parameters like a method and run logic like a method. You can think of it as the "Main" method of the class.

 

When should you use a constructor?

Like many things in programming, its not necessarily about better or worse code. It can just be about how you like to do things and what you consider more efficient / practical / functional. Many programming techniques are more due to the programmer's style than to a tangible benefit at run-time.

To answer the question, if you want something to happen when a class is first created then make a constructor for it and handle it all in there.

 

Where do I put the constructor?

It goes in the class, in this class the HSLColorHandler constructor would be inside HSLColorHandler.

 

So whats the point of having methods?

While classes serve as a program wide solution, methods are a more local layer of abstraction. They allow you to split a task within a class into sub tasks. They much like the class are reusable and are simply called this way: methodName(parameters);

Methods can return variables as stated earlier.

 

I am calling a method but it is not working!!

Well you need to watch out for something. When accessing methods or variables from another class you need to make sure what you are accessing is public. There are other ways but they are considered hacky and are out of the scope of this brief overview.

 

You see in the class I have attached, some methods are private, others are public. Same for variables. Anything private is only accessible from within the class itself. Public stuff can be reached from anywhere.

This is why you should always use getters and setters which I'll go over in a second. Once more, the use of private public can come down to the programmer's preference. Especially at the start of their career however as you progress you will realise how useful it is to manipulate them correctly until it becomes a necessity. It is good practice to begin now.

 

When should a method / variable be private?

If you only plan on accessing it from within the class then there is no reason for it to be public!! It will make accessing the class from the exterior more complicated. Remember the goal is to keep as much of the stuff happening inside the class right where it is and not burden the programmer with its complexity. To this end minimizing interraction is a must.

Look at the code I attached. I don't require a user to access each individual method, rather I handle it all within the class. The flow of data is newC() >> makeRGB(float[] hsl) >> HueToRGB(float p, float q, float h) >> newC().

 

So when should I make it public?

To interact with the class you do need the means to do so. Make public what needs to be accessed from the exterior, try to keep the flow of data and logic of the system contained to the class.

Variables should almost never be public!! Unless you are purposefully hacking something together which is not advised either. We all do it occasionally though.

 

I sometimes see 2 methods inside a class using the same name; how does that work?

I forget the terminology but it is essentially a run-time switch. As long as all the methods sharing a name have different parameters you are good to go. You will need to access the methods with care though since parameters input to you method call will select which method is called. This is a more efficient (in terms of resources) way of handling certain situations.

 

What is the point of Getters and Setters?

These are methods, inside a class and they replace direct access to a variable. Yes you could make the variable public but this is against the principle of abstraction. There are a bunch of reasons for using getters and setters, just google it. In the meantime trust me. Use them!!!

 

Hope this helped, I am not sure how much you knew and needed to know. I have just explained what I have noticed people had trouble with.

**I am also a student, in fourth year. I know I probably messed some stuff up so correct me please!**

I also barely scraped the surface but it should be a good starting point.

 

Further reading:

Abstraction

How to design OO architecture

OO paradigm

Link to comment
Share on other sites

Link to post
Share on other sites

If you want a more in depth explanation about anything specific go ahead and ask.

Link to comment
Share on other sites

Link to post
Share on other sites

codecademy had a pretty good Java explanation of classes. I learned it here, (as in on this forum), and codecademy then by practicing. 

It's kinda funny you make this thread... I'm working on a youtube tutorial to explain classes since it was so difficult for me to get them. Then once I got them, I got them. 

Link to comment
Share on other sites

Link to post
Share on other sites

12 hours ago, fpo said:

codecademy had a pretty good Java explanation of classes. I learned it here, (as in on this forum), and codecademy then by practicing. 

It's kinda funny you make this thread... I'm working on a youtube tutorial to explain classes since it was so difficult for me to get them. Then once I got them, I got them. 

What is so difficult to understand? Classes are just static containers to organise your code. A runtime environment (virtual machine or interpreter) may invoke the constructor of a class in order to instantiate an object, which has a state and implements behavior through methods.

Link to comment
Share on other sites

Link to post
Share on other sites

I'm a little confused about your question, but if you are asking how to call methods from other classes you need to provide a constructor. I'll leave a little example here.

 

public class Example {

    int myInteger = 0;

    // Below here is your default constructor, this is required for creating an instance of this class. It allows you to call methods from another class.
    // You can (and many times will), specify required parameters for construction. These parameters can be any variable or even another class. 
    //Generally beginners don't need to worry about this.
    public Example(){

    }

    // Now that you have a constructor, you need to make methods. Methods can return variables, or they can return nothing (void).
    // They can also return values which you will see in the next example.

    public void addOne(){
    myInteger ++;
    }
    
    // This is a method commonly referred to as a getter. It allows you to access variables from other classes. 
    // Inversly, you can also define a method called a "setter" which allows you to manipulate variables from other classes.
    public int getMyInt(){
        return myInteger;
    }
    
}




    // Okay, so here is how you would call the method above from another class.
    
public class Example2{
    // Define a new Example class.
    Example foo;
    
    public static void (String[] args){
        // construct the class using the default constructor we used in the previous code.
        foo = new Example();
        
        foo.addOne(); // This is calling the method addOne in the example class.
        
        System.out.println(foo.getMyInt()); // This is calling the method getMyInt from the example class.
    }
    
}

 

CPU: Ryzen 5950X Ram: Corsair Vengeance 32GB DDR4 3600 CL14 | Graphics: GIGABYTE GAMING OC RTX 3090 |  Mobo: GIGABYTE B550 AORUS MASTER | Storage: SEAGATE FIRECUDA 520 2TB PSU: Be Quiet! Dark Power Pro 12 - 1500W | Monitor: Acer Predator XB271HU & LG C1

 

Link to comment
Share on other sites

Link to post
Share on other sites

15 hours ago, TheHoijf said:

I sometimes see 2 methods inside a class using the same name; how does that work?

I forget the terminology but it is essentially a run-time switch. As long as all the methods sharing a name have different parameters you are good to go. You will need to access the methods with care though since parameters input to you method call will select which method is called. This is a more efficient (in terms of resources) way of handling certain situations.

 

It's called method overloading. The method signature varies only by its parameters.

You can't say that it is more efficient, be careful with statements like that regarding compiled code. For example (long running) Java Bytecode is heavily optimised by the JIT-Compiler.

 

15 hours ago, TheHoijf said:

What is the point of Getters and Setters?

These are methods, inside a class and they replace direct access to a variable. Yes you could make the variable public but this is against the principle of abstraction. There are a bunch of reasons for using getters and setters, just google it. In the meantime trust me. Use them!!!

 

Wrong! The term is called encapsulation and the purpose is to separate the implementation from the interface or abstraction. NEVER declare an instance variable as public, it's very bad noob-practice! Public instance variables will be exported with the API of your class/type. You want to hide the implementation so you can adapt/improve it without breaking existing code.

 

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, Remixt said:

I'm a little confused about your question, but if you are asking how to call methods from other classes you need to provide a constructor. I'll leave a little example here.

 


public class Example {

    int myInteger = 0;

    // Below here is your default constructor, this is required for creating an instance of this class. It allows you to call methods from another class.
    // You can (and many times will), specify required parameters for construction. These parameters can be any variable or even another class. 
    //Generally beginners don't need to worry about this.
    public Example(){

    }

    // Now that you have a constructor, you need to make methods. Methods can return variables, or they can return nothing (void).
    // They can also return values which you will see in the next example.

    public void addOne(){
    myInteger ++;
    }
    
    // This is a method commonly referred to as a getter. It allows you to access variables from other classes. 
    // Inversly, you can also define a method called a "setter" which allows you to manipulate variables from other classes.
    public int getMyInt(){
        return myInteger;
    }
    
}




    // Okay, so here is how you would call the method above from another class.
    
public class Example2{
    // Define a new Example class.
    Example foo;
    
    public static void (String[] args){
        // construct the class using the default constructor we used in the previous code.
        foo = new Example();
        
        foo.addOne(); // This is calling the method addOne in the example class.
        
        System.out.println(foo.getMyInt()); // This is calling the method getMyInt from the example class.
    }
    
}

 

 

You don't need to define an empty default constructor. The Java and .NET compilers do it for you.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

25 minutes ago, Audio Carnage said:

 

You don't need to define an empty default constructor. The Java and .NET compilers do it for you.

 

 

It's good practice to do it yourself.

CPU: Ryzen 5950X Ram: Corsair Vengeance 32GB DDR4 3600 CL14 | Graphics: GIGABYTE GAMING OC RTX 3090 |  Mobo: GIGABYTE B550 AORUS MASTER | Storage: SEAGATE FIRECUDA 520 2TB PSU: Be Quiet! Dark Power Pro 12 - 1500W | Monitor: Acer Predator XB271HU & LG C1

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Remixt said:

It's good practice to do it yourself.

No it is not.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Nuluvius said:

No it is not.

There are a lot of API's (Especially the Google APIs) that require you to have an empty default constructor. Professors teach students to use one when first learning programs. It is good practice. At least here in the U.S.

CPU: Ryzen 5950X Ram: Corsair Vengeance 32GB DDR4 3600 CL14 | Graphics: GIGABYTE GAMING OC RTX 3090 |  Mobo: GIGABYTE B550 AORUS MASTER | Storage: SEAGATE FIRECUDA 520 2TB PSU: Be Quiet! Dark Power Pro 12 - 1500W | Monitor: Acer Predator XB271HU & LG C1

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Remixt said:

There are a lot of API's (Especially the Google APIs) that require you to have an empty default constructor. Professors teach students to use one when first learning programs. It is good practice. At least here in the U.S.

In the cases where it is required then that is a different story as there is a fundamental need for it to be there. In this particular case however, since we are talking about a language feature, that is provided by default, then it is not good practice at all to explicitly re type the thing. Why would you wast your time hammering on the keyboard for that? It doesn't add any readability, all it does is waste screen space and add to the entropy of the design. Moreover anyone who reads code like that is almost certainly going to be questioning the competence of the one who was responsible for writing it as it clearly shows a poor understanding of the language!

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Remixt said:

There are a lot of API's (Especially the Google APIs) that require you to have an empty default constructor. Professors teach students to use one when first learning programs. It is good practice. At least here in the U.S.

Maybe in C++ (I don't remember, it's been too long), but most other OOPs after that implicitly defines a default constructor that's empty.

 

Like how there's an implicit "this" in every method of a C++ object. Which annoys me in Python 2.x because you have to add in "self" to all your class's methods.

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, Audio Carnage said:

What is so difficult to understand? Classes are just static containers to organise your code. A runtime environment (virtual machine or interpreter) may invoke the constructor of a class in order to instantiate an object, which has a state and implements behavior through methods.

The part on how you can create instances of classes is difficult. 

Link to comment
Share on other sites

Link to post
Share on other sites

15 hours ago, Audio Carnage said:

Wrong! The term is called encapsulation and the purpose is to separate the implementation from the interface or abstraction. NEVER declare an instance variable as public, it's very bad noob-practice! Public instance variables will be exported with the API of your class/type. You want to hide the implementation so you can adapt/improve it without breaking existing code.

 

That's what I meant. Sorry for the confusion. Definitely better said than me ^^. Getters and setters are a must. Even when you don't plan on making changes to something or ever coming back to it, you should use them just in case.

Admittedly, I have ignored them at times without any loss of functionality or time but only in sub thousand line programs which were made for use by me and solely by me (even here it is silly but whatever, it worked out). I would never skip on it when working on professional or academic work and would advise others to do the same. They have become a standard for a reason!!

 

As for the " I sometimes see 2 methods inside a class using the same name; how does that work? " section; it can mean multiple methods which do the same thing but require different variables could be made to share a method name thus avoiding confusion from the programmer's end. In that sense it is sometimes beneficial.

I rarely if ever use it but the practice does take place so I thought I'd mention it.

 

I think for a first year exam, knowing the little block I wrote + any corrections made would be a good start!!

 

Keep in mind, Cookiecrumbles222 is only looking for some relatively nooby information!! He will have his whole career to delve into intricacies. For the time being, surface knowledge is certainly adequate. I was only taught about public / private and void / returns in second year!!

 

I came back to mention something else though.

I am unsure whether this is in the scope of OO but it won't hurt to know about it.

These are programming patterns which you often encounter in OO. They are very much worth looking into because not only will you learn about the patterns but other underlying aspects of OO.

 

Go to Stack Overflow and check out some threads there concerning OO. The forum is catered to users with at least medium level experience but I sure found it helpful when I had just started.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, TheHoijf said:

Admittedly, I have ignored them at times without any loss of functionality or time but only in sub thousand line programs which were made for use by me and solely by me (even here it is silly but whatever, it worked out). I would never skip on it when working on professional or academic work and would advise others to do the same.

If you 'would never skip on it when working on professional or academic work' then why have you allowed yourself to do so for your own projects? In my opinion this is simply sloppy and representative of a lack of understanding and/or appreciation firstly for the language feature itself and secondly for best practices and patterns. If one does not cultivate good habits when they have the opportunity to practice then it's arguably going to bleed through to when it really matters to do so. I have absolutely no toleration or sympathy for you. Moreover you are compounding your obvious lack of knowledge by making such woolly and blatantly incorrect statements such as:

2 hours ago, TheHoijf said:

As for the " I sometimes see 2 methods inside a class using the same name; how does that work? " section; it can mean multiple methods which do the same thing but require different variables could be made to share a method name thus avoiding confusion from the programmer's end. In that sense it is sometimes beneficial.

This is called overloading and setting aside the fact that you have already been called out on this once before, no they do not have to 'do the same thing' at all. There's absolutely nothing about the concept of overloading that enforces that the implementation details remain the same between methods.

2 hours ago, TheHoijf said:

I rarely if ever use it

That doesn't surprise me in the least since you have clearly demonstrated a very poor grasp of it...

2 hours ago, TheHoijf said:

I came back to mention something else though.

I am unsure whether this is in the scope of OO but it won't hurt to know about it.

These are programming patterns which you often encounter in OO. They are very much worth looking into because not only will you learn about the patterns but other underlying aspects of OO.

Honestly it's as if you just buzz worded design patterns and linked up some random examples... Do you really even understand what you have listed? Just about the only reason to even list the Singleton Pattern is to get an appreciation of just how bad it really is and how much one should do everything that they possibly can to avoid it's use. I'd be seriously concerned if I were to be encountering that particular pattern often, as you put it...

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Nuluvius, you aren't being helpful, only pointing out what you consider mistakes although I would consider this more in the realm of technicalities.

Why so aggressive anyway? My information has at no time been incorrect and you seem to be picking it apart for the sake of it.

Why don't you suggest good starting points? Make a comprehensive summary like I did and enlighten us all? If I am so deeply mistaken I would definitely appreciate the clarification. So far, you have failed to teach me anything.

 

The patterns I listed are used regularly and I was taught them at university so I thought I would link them. The professors in my university consider them good enough to center a module around them, why don't you?

They may have their flaws but it is good to know about them because they have a tendency to open one's eyes to new practices, show possibilities.

 

Allow me to clarify.

 

I'd like to start off by saying, books have been written about this. Many BIG books. I am trying to be concise, please use your common sense when reading this. If concise just won't cut it and you need multiple pages of text then please, go get a book (I am talking to you Nuvilius).

 

Overloading

Definition from BeginnersBook

Quote

Method Overloading is a feature that allows a class to have two or more methods having same name, if their argument lists are different.

You can do whatever you want in each method, for all intents and purposes they are completely separate methods within the class, just like any other randomly chosen methods in the class. The similarity between them (the name) is only for the reader.

The point of using overloading is to associate methods which share some similarities. You don't need to use it this way, but it is intended to be used this way.

Example:

private int add(int[] a)
{
	int b = 0;
	for(int i = 0; i < a.length; i++)
	{b += a[i];}
	return b;
}

private int add(int a, int b, int c)
{
	return a+b+c;
}

You can also have one draw a cow on the screen and one calculate prime numbers, if you really want to.

In this sense, I would say Nuvilius has a poor grasp of the practice. While the possibility is there, that is not its use case and shouldn't be used that way.

 

Why do I rarely use it?

Because, I prefer to edit the name. For example, here I would have add_A and add_B. Why? Well I find it easier to find my way through code this way. The eclipse auto highlight makes it easy to find what I am looking for if I differentiate things when possible. I could use Javadoc, which I do, but that is different.


Getters and Setters

Everything I said in my first post about them remains correct. They should always be used on serious work. However if you are testing something, I personally don't think its worth the time. They only ever become useful once the calls to the variable are so numerous that you would actually save time by using getters and setters. The more you access a given variable the more necessary it is to have getters and setters.

What is the fundamental reason for using them though? Well, if you have a variable in class A which you want to access from classes B, C, D. You never know what the future holds for our variable and any change to it would mean you'd also need to change classes B, C and D. To avoid this, you can implement a getter and instead of changing B, C and D you can just edit the getter. In the long run it will save a huge amount of time. On tiny projects however it will use more time than it takes. In my experience this has been the case, maybe I just plan things out better than most.

Sometimes people also use them as a point for validation or other tasks, I however prefer to do this on the other end. When resources are limited (weak computers) you want to minize processing. If classes B and C need validation on the variable but not D then I would create a validation class which would be called by B and C, in this way, D wouldn't waste time validating the variable since he doesn't need to.

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, TheHoijf said:

Nuluvius, you aren't being helpful, only pointing out what you consider mistakes although I would consider this more in the realm of technicalities.

5 hours ago, TheHoijf said:

My information has at no time been incorrect and you seem to be picking it apart for the sake of it.

I'm pointing out your mistakes, these are not technicalities, subjective or otherwise opinion based, they are where you have been fundamentally incorrect. Trying to gaslight the fact that you have been wrong only serves to further compound your ignorance. In any event the rest of your points are so woolly and nebulous that relatively little information of any value is actually conveyed at all. Maybe you should be pursuing politics instead...

On 06/01/2017 at 0:13 AM, TheHoijf said:

**I am also a student, in fourth year. I know I probably messed some stuff up so correct me please!**

A bit hypocritical don't you think.

5 hours ago, TheHoijf said:

Why so aggressive anyway?

Where was the aggression? It seems as though, despite requesting that we correct you, you have a rather difficult time accepting criticism when you have been wrong. This type of attitude will not get you very far once or indeed if you ever reach industry because you will know precisely nothing.

5 hours ago, TheHoijf said:

The patterns I listed are used regularly and I was taught them at university so I thought I would link them. The professors in my university consider them good enough to center a module around them, why don't you?

That's the problem. Go and have a read of this thread because I honestly can't bothered and don't have the time to type it all out again for your benefit.

 

Quintessentially in the area of theory they usually do a piss poor job of preparing people for current industry. Listing the Singleton pattern as a go to is a perfect example of this. You have demonstrated obsolete knowledge, deprecated and simply bad practices and yet you have the audacity to try to insult me when I have called you out on them... xD

5 hours ago, TheHoijf said:

Why don't you suggest good starting points? Make a comprehensive summary like I did and enlighten us all?

Why should I? I neither have the time nor the inclination right now as I am preparing for yet another surgery. The only ones here that I can see to be in need any kind of 'enlightenment' are yourself and the OP - at least the OP acknowledges where knowledge is lacking and does not attempt to bury the fact under a mountain of verbosity. In an event isn't it nice to let other people have a turn once in a while, I thought so at least.

 

If you want to learn about theory, specifically theory that is currently relevant then you'd do far better to get yourself out of that already deprecated mindset that you are currently entrenched in i.e. coveting books as a source on information that is by it's very nature inherently fluid and in a constant state of change and evolution. It's ludicrous but very typical of a student mindset I suppose.

5 hours ago, TheHoijf said:

So far, you have failed to teach me anything.

The most concise piece of fundamental theory that you have thus far missed is Single Responsibility, Open-Closed, Liskov Substitution, Interface Segregation and Dependency Inversion (SOLID). We create classes because we have a concept or a concern that we want to encapsulate, we want to provide an interface for interacting with it to the outside word that makes some meaningful sense and we want to hide the details of it's implementation away. A class should do only one single thing or deal with only one single concern and no more. More generally this should also apply to anything one writes; a method, a class or a library; to do otherwise is a violation of the Single Responsibility Principal and is indicative of a poor design. This should be so deeply ingrained into one such that any code that they write adheres to these core principles by de facto standard regardless of it's setting or intention.

 

Principals such as the Dependency Inversion Principal, Dependency Injection (DI)Inversion of Control (IoC) and Composition over Inheritance (where it makes sense) are important for avoiding coupling i.e. where class A owns class B we would instead seek to provide class B to class A. This lends itself to producing better, more extensible, easier to maintain and highly testable code which leads onto driving the process by Test Driven Development (TDD) and arguably the better approach of Behaviour Driven Developmental (BDD) with specific use of the Ports and Adaptors Architecture.

 

When we talk about Design Patters we are referring to topological ways of organising code (such as classes) into meaningful and standardised structures within a given programming paradigm and each paradigm has it's own set of design patterns and principals. These patterns make up the overall architecture of the software. There are good and bad patterns, the bad ones are often refereed to as Anti-patterns such as is the case with the Singleton Pattern. Here's a great article on why the Singleton Pattern is crap and should be avoided:

Quote

In short, the singleton pattern makes code more complex, less useful, and a real pain to re-use or test. Eliminating singletons can be tricky, but it’s a worthwhile endeavour.

All you did was real off a load of very specific and loosely related buzz words without providing a context or even a generalised overview... Followed by a lot of obnoxious noises when you were called out on it.

 

In any event we have digressed well beyond the realms of relevance to the OP's original question:

On 04/01/2017 at 5:09 AM, Cookiecrumbles222 said:

hay guys i'm in java/c++ classes and the current topic is classes, i'm not sure if that the right world, but its the thing where you make two separate class projects and to make public and private functions and then call on them in the other page (example below). but i have no idea how to do it and i'm falling behind, so if anyone knows a good why to explain this stuff or where i can find how to do it, it would be much appreciated. thank you!

Which I think can simply be distilled down to: Technically how do I go about creating and using classes in Java and C++ and where can I go for more information?

 

The latter can be found in many of the beginner videos on somewhere such as Pluralsight for example. While the former can simply be accomplished thus:

Spoiler

C++:

Spoiler

SomeClass.h



#pragma once

#include <string>

class SomeClass
{
public:
	std::string SomeString();
};

SomeClass.cpp



#include "SomeClass.h"

std::string SomeClass::SomeString()
{
	return "Some String";
}

Main.cpp



#include "SomeClass.h"
#include <iostream>

using namespace std;

int main()
{
	SomeClass someClass;

	cout << someClass.SomeString() << endl;
	cin.get();

    return 0;
}

 

Java:

Spoiler

SomeClass.java



public class SomeClass
{
    public String someString()
    {
        return "Some String";
    }
}

Main.java



import java.io.IOException;

public class Main {

    public static void main(String[] args)
    {
        SomeClass someClass = new SomeClass();
        System.out.println(someClass.someString());

        try
        {
            System.in.read();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

 

 

Further technical topics for you to research would be:

  • Reference and value types
    • Passing by reference and by value
  • Pointers
  • Const correctness
  • Header and source file types (specifically applicable to C++)
  • Access modifiers

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×