How to make flow chart and write code for actual idea ?

Thread Starter

John99407

Joined Jul 12, 2019
77
I want to try coding for real life example. I want to write code for OOP features in C++ language. Vehicle is most common example to learn object oriented programming. I want to use vehicle example to learn coding for encapsulation, inheritance and polymorphism

Vehicles: bicycle, bike, car, bus, truck, train, aircraft
Common Vehicles
: (car, bus, truck)

All Vehicles have some similarities
Common Vehicles: wheels, breaks, steering wheel

Every car have wheels, breaks, steering wheel
Every bus have wheels
breaks, steering wheel
Every truck have wheels
breaks, steering wheel

All vehicles are different from each other
Vehicles Property:
speed, wheel size, window size, fuel capacity.

Speed of vehicles may be different then other vehicles
Wheel size of vehicles may be different then other vehicles
Window size of vehicles may be different then other vehicles
Fuel capacity of vehicles may be different then other vehicles


I have define class vehicle
Code:
class Vehicles {
  Wheels
  breaks,
  steering wheel
  }
};
All those three are the object of class
Vehicles

inheritance Examples
: a car is vehicle, truck is vehicle but car is not a truck



Code:
class Vehicles {
Wheels
breaks,
steering wheel
}

class Car : public Vehicles{
Wheels
breaks,
steering wheel
}
};
Polymorphism
Code:
class Vehicles {
  Wheels
  breaks, 
  steering wheel
  }

class Car {
  Wheels
  breaks, 
  steering wheel
  }
};

I am looking assistance who can guide me.

I am struggling to convert idea into coding. How to convert real time idea into coding

john
 

BobaMosfet

Joined Jul 1, 2009
2,110
Learn C/C++. Don't ask people to teach you, buy a book (Amazon?), Look on Google. There are coding forums. Join your local developer or electronics club and ask if they have anyone who might be willing to teach you.

Beyond that, learn to create a flow chart. Pencil and Paper. Most people don't understand what code is. Code, in any computer language is nothing more than a grammar-based language designed to tell the compiler how to write code in a format the machine can parse (whether interpreted or compiled).

Code is logic in written form. Logic is algorithm. How things are done conceptually. This is why flowcharting is important. Some people will tell you that you must learn a language to flow-chart. NO! You need only paper and pencil and the ability to draw a box and an arrow and annotations along the way so that anyone could look at your drawing and say, "Oh, here is the start, and if I go through it step by step, and I can see the processes and decisions made along the way to get to the end."

Logic can be coded in any language.

Once you have a flowchart done, you can walk through it and test it on paper. If you can find now flaws, you are ready to code that into whatever language you choose. Flowcharting will help you organize your process and identify sub-processes, which can become functions or procedures. It can help you get your idea out of your head in an understandable way, freeing your mind to work on the problem, not on holding onto the process. And it can help you see fundamental flaws, or entirely new ways to do something because you have an Aha moment and say "Ahhh!! I can do that simpler, this way!".

Anybody who doesn't value flow-charting for the incredibly powerful tool it is, is not a professional and does not seek to be. In some companies, they require their developers to create a flow-chart with every single function and code-piece- for the archival data documentation.
 

Ian Rogers

Joined Dec 12, 2012
1,136
When using inheritance, you create a class from a class using "other" properties.
so:-
Code:
class Vehicles {  
   Wheels,  
   brakes,   
   steering wheel  
};
class Car : public Vehicles {  
  seats,
  radio,
};
The class Vehicle already defines wheels, brakes and steering wheel so you inherit them.
Polymorphism is overriding the inherited classes functions ie.. Steering wheel on a tank may be two levers... so steering wheel would come under direction control, this would be the "redefinition" needed for new classes built on the Vehicles class..

Encapsulation just means object.. All functions and variables all encapsulated into a single variable ie.. class..
 

KeithWalker

Joined Jul 10, 2017
3,063
I want to try coding for real life example. I want to write code for OOP features in C++ language. Vehicle is most common example to learn object oriented programming. I want to use vehicle example to learn coding for encapsulation, inheritance and polymorphism

Vehicles: bicycle, bike, car, bus, truck, train, aircraft
Common Vehicles
: (car, bus, truck)

All Vehicles have some similarities
Common Vehicles: wheels, breaks, steering wheel

Every car have wheels, breaks, steering wheel
Every bus have wheels
breaks, steering wheel
Every truck have wheels
breaks, steering wheel

All vehicles are different from each other
Vehicles Property:
speed, wheel size, window size, fuel capacity.

Speed of vehicles may be different then other vehicles
Wheel size of vehicles may be different then other vehicles
Window size of vehicles may be different then other vehicles
Fuel capacity of vehicles may be different then other vehicles


I have define class vehicle
Code:
class Vehicles {
  Wheels
  breaks,
  steering wheel
  }
};
All those three are the object of class
Vehicles

inheritance Examples
: a car is vehicle, truck is vehicle but car is not a truck



Code:
class Vehicles {
Wheels
breaks,
steering wheel
}

class Car : public Vehicles{
Wheels
breaks,
steering wheel
}
};
Polymorphism
Code:
class Vehicles {
  Wheels
  breaks, 
  steering wheel
  }

class Car {
  Wheels
  breaks, 
  steering wheel
  }
};

I am looking assistance who can guide me.

I am struggling to convert idea into coding. How to convert real time idea into coding

john
What do you want your program to do when it is complete?
You need to define what the inputs and outputs are and what happens in between. Then you can create a high level flow chart of the program. The next step is to take each high level block of the chart and break it down to the next level of functionality. When you have broken down every function as far as you can go, then you have a complete flow chart of your program and you can start writing the code.
Start by writing the modules for the lowest level of functionality first and test each module when it is complete. When you have finished the lowest levels, start the next level up for each block by testing the lower level functions together to make sure they interact correctly. Continue in this manner until you have completed everything including the highest levels. See the enclosed example:
 

Attachments

Thread Starter

John99407

Joined Jul 12, 2019
77
I have implemented idea for inheritance in coding. I am creating new class Car from Vehicle class
Code:
#include <iostream>
#include <string>
using namespace std;

// Base class
class Vehicle {
  public:
    int Wheels;
    int brakes;  
    int steeringWheel;
    };
   
// Derived class
class Car: public Vehicle {
  public:
      int seats;
      string radio = "Music";
};

int main() {
   Car BMW;
   BMW.seats = 4;
   BMW.radio;
 
   cout << "BMW Seats : " <<BMW.seats << endl;
   cout << "BMW Radio : " <<BMW.radio << endl;

  return 0;
}
 

Ian Rogers

Joined Dec 12, 2012
1,136
Okay... Now you need to hide the variables from the outside world.. ( The reason for oop in the first place.)

You write functions to modify and expose these variables... or else it may just as well be a simple structure..
 

xox

Joined Sep 8, 2017
838
One thing to always keep in mind is that OOP is just a tool and not merely an end to a means. You don't necessarily have to model everything as an object. In other words, if it makes better sense to solve a problem with object-oriented programming, do that. Otherwise if functional programming feels right for some other job then use that. Or maybe some other kind of approach. There are many.

Anyway with C++ you can either derive with or without virtual functions. Without them you can only use functionality available from the base class. They can however still be used to call virtual methods so they aren't entirely useless either.

As for public data members these are seldom useful and unless you have a really good reason to make them public, don't. Because you can't monitor them reliably. Too easily modified by users in haphazard ways and it just gets messy. That's why classes are best off going with a strictly all-method public interface while structs are usually used where no methods are desired. Just plain data structures to pass around to methods as settings, etc.

Virtual functions can be overridden to create really interesting behavior. But it gets tricky. You can't simply declare a 'Car' object and push it onto a 'vector<Vehicle>'. Normally the only real option is to use dynamic allocation. You push a 'new Car(...)' onto a 'vector<Vehicle*>' (or better yet a 'vector<my_whatever_smart_pointer<Vehicle>>') and voila.

Another interesting type of polymorphism is found in template metaprogramming. Thanks to inlining they can be designed to run with practically ZERO overhead compared with the preformance hit of virtual functions. You still can't push them directly onto a vector (in that case a virtual class wrapper would be needed) but they are still infinitely useful for various single-instance situations. It's called the "curiously recursive template idiom", in case you're interested.

Beyond that I have to echo what has already been said here. Read a book or two before you start on this. C++ is a complex language and it's frustratingly easy to screw things up. Learn all of the basics and as many of the finer points you can manage to grasp. It takes time and patience but it does pay off in the end.
 
Last edited:

Thread Starter

John99407

Joined Jul 12, 2019
77
Okay... Now you need to hide the variables from the outside world.. ( The reason for oop in the first place.)

You write functions to modify and expose these variables... or else it may just as well be a simple structure..
I can hide data by making private
C:
#include<iostream>
#include<string>

using namespace std;

// Base class
class Vehicle {
  public:
    int Wheels;
    int brakes; 
    int steeringWheel;
    };
  
// Derived class
class Car: public Vehicle {
  private:
   // data hidden from outside world
      int seats;
  public:
     string radio = "Music";
      
      void Seats (int S) {   
         seats = S;
     }
    
     int getSeats() {     
       return seats;         
     }
};

int main() {
   Car BMW;
   BMW.radio;
   BMW.Seats(4); 
   cout << "BMW Seats : " <<BMW.getSeats() << endl;
   cout << "BMW Radio : " <<BMW.radio << endl;
 
  return 0;
}
 

Thread Starter

John99407

Joined Jul 12, 2019
77
Learn C/C++. Don't ask people to teach you, buy a book (Amazon?)
Read a book or two before you start on this. C++ is a complex language and it's frustratingly easy to screw things up.
I didn't reply to BobaMosfet post because I think he is scolding me like why you are asking If you have a book then read it.

Yes I have a book and I'm reading it. Now the question is , why did I ask a question here because I think it's batter to implement real time idea come in mind into code. I explained whatever I had in my mind and I tried to implement logic into code. I never said that write code for my logic
. All I am looking is guidance and advice to develop program.
 

KeithWalker

Joined Jul 10, 2017
3,063
I decided to create a flow chart of the program first , but I'm not sure how to represent a class. How do I represent access specifier data member and member function in class

Can someone show me Flowchart that use classes ?
You are starting your project from the wrong end. Forget "classes" and all the other functionality of your OOP. You don't need them to create a flow chart. The flow chart shows the flow and functions of the program. Define what your program is going to do, then break it down into what is required to achieve it. When complete, the program can be written in any language (including your OOP), using the flowchart as a guide.
 

Thread Starter

John99407

Joined Jul 12, 2019
77
You are starting your project from the wrong end. Forget "classes" and all the other functionality of your OOP. You don't need them to create a flow chart. The flow chart shows the flow and functions of the program. Define what your program is going to do, then break it down into what is required to achieve it. When complete, the program can be written in any language (including your OOP), using the flowchart as a guide.
Can someone show me how can we make flow chart for class ?

I made it this one but it's not meaningful

1574534245060.png
 

KeithWalker

Joined Jul 10, 2017
3,063
You are correct. It does not show how anything happens. As I stated above, a flow chart shows the flow and functions of the program. Define what your program is going to do, then break it down into what is required to achieve it.
"Class" is a blueprint for creating objects. To make a flowchart for it you must define what it does, what inputs it needs to do it and what the end product is. It must show all the decisions and branches it uses to get there.
I have done the first step for you in defining it. Now it's up to you to do the rest using the guide lines I have given you.
 

Thread Starter

John99407

Joined Jul 12, 2019
77
You are correct. It does not show how anything happens. As I stated above, a flow chart shows the flow and functions of the program.
I have done the first step for you in defining it. Now it's up to you to do the rest using the guide lines I have given you.
I find difficult to make flow chart for class. I don't have any idea how to represent function and flow of program. I have already shown you my best attempt to make flow chart.

I looked into your flow chart It also doesn't show function in program. can you make one flow chart for class ?
 

KeithWalker

Joined Jul 10, 2017
3,063
I find difficult to make flow chart for class. I don't have any idea how to represent function and flow of program. I have already shown you my best attempt to make flow chart.

I looked into your flow chart It also doesn't show function in program. can you make one flow chart for class ?
From the chart you made, I can see that you are still tackling the problem from the wrong end. Your chart does not show how "class" works. It shows what kind of data you want to put into it. A flowchart should show how it would handle any data and what it would do with it.
The partial flowchart I made as a simple example shows the function of an environmental monitoring system. It shows what the inputs are, what happens to data from the inputs, what decisions are made to get the data and what the output is. Lower levels of the chart would define more detail.
I have no reason to make a flowchart for "class". It would take up far too much of my precious time to research how it works an what it does.
Before you make a flowchart, you need to write a specification for the process you are going to chart. It should define the inputs, outputs and functionality. Then you know what your flowchart has to include.
If you still don't understand how to make a flowchart, it's time you did some reading on the subject.

https://en.wikipedia.org/wiki/Flowchart
 
Last edited:

SamR

Joined Mar 19, 2019
5,031
I sucked at flow charts decades ago in school and hated them. On the first day of class, they gave all of us a plastic flow symbol template to use when drawing the chart. They are a useful exercise for developing program structure. Think of it as a decision tree, when this happens what do I do in response, if it doesn't happen what do I do. Keep doing that until you "run out of ideas" and at that point, you have no further tasks to perform coding for. From that skeletal chart start coding how to perform each if/then until you have reached the end of the chart. It gets easier with practice and I still hate it. Think of it as a very necessary evil to be done away with as quickly as possible. The final program will never match the original chart as you debug it and perfect it.
 

atferrari

Joined Jan 6, 2004
4,764
I sucked at flow charts decades ago in school and hated them. On the first day of class, they gave all of us a plastic flow symbol template to use when drawing the chart. They are a useful exercise for developing program structure. Think of it as a decision tree, when this happens what do I do in response, if it doesn't happen what do I do. Keep doing that until you "run out of ideas" and at that point, you have no further tasks to perform coding for. From that skeletal chart start coding how to perform each if/then until you have reached the end of the chart. It gets easier with practice and I still hate it. Think of it as a very necessary evil to be done away with as quickly as possible. The final program will never match the original chart as you debug it and perfect it.
A proper flowchart, in my experience, makes of writing actual code just a clerical job. I like that.

Found them a good help when revisiting old code.

For debugging I keep it side by side with code itself just to discard where the error is not.
 

Thread Starter

John99407

Joined Jul 12, 2019
77
I have no reason to make a flowchart for "class". It would take up far too much of my precious time to research how it works an what it does.
Before you make a flowchart, you need to write a specification for the process you are going to chart. It should define the inputs, outputs and functionality. Then you know what your flowchart has to include.
If you still don't understand how to make a flowchart, it's time you did some reading on the subject.
https://en.wikipedia.org/wiki/Flowchart
You are taking me to different discussion. My original question was how to write code for real time idea. I have already made some progress on it. you suggested me to draw flow chart so I spent my time to make flow chart and I posted one but I think it's easy to write code instead of making flow chart that's why no one is showing their flow chart.

It would have been benefit if you had shown me a flow chart for class, I might have done something next
 

KeithWalker

Joined Jul 10, 2017
3,063
You are taking me to different discussion. My original question was how to write code for real time idea. I have already made some progress on it. you suggested me to draw flow chart so I spent my time to make flow chart and I posted one but I think it's easy to write code instead of making flow chart that's why no one is showing their flow chart.

It would have been benefit if you had shown me a flow chart for class, I might have done something next
I still don't understand what you are trying to do. You can't write code for an "idea" until you have defined what it is and what it is supposed to do. You have not told us that. You just seem to be kicking bits of data around in a random fashion. It would be a waste of time to try to put that into code. I think no one else is getting involved because thay dont understand your question. What exactly are you trying to achieve?
 
Top